0

How to pass a parameter to kendo dialog action method? I am trying to pass action_name variable as a parameter to close action method but it is not working.

 @{
    var action_name = "delete";
  }

@(Html.Kendo().Dialog()
    .Name("dialog")
    .Title("Software Update")
    {
        actions.Add().Text("Ok");
        actions.Add().Text("Cancel").Action("onCancel(" + action_name + ")");
    })
)

<script type="text/javascript">
    function onCancel(action_name) {
        alert(action_name);
    }
</script>

1 Answer 1

0

To pass a parameter to the Kendo Dialog action method, ensure the parameter is correctly formatted as a string in JavaScript. Based on your code, you need to wrap parameter in quotes:

@{
    var action_name = "delete";
}

@(Html.Kendo().Dialog()
    .Name("dialog")
    .Title("Software Update")
    .Actions(actions => 
    {
        actions.Add().Text("OK");
        actions.Add().Text("Cancel").Action("onCancel('" + action_name + "')"); // Wrap parameter in quotes
    })
)

<script type="text/javascript">
    function onCancel(action_name) {
        alert(action_name); // Will display 'delete'
    }
</script>

Using the above code, the onCancel method will trigger when the page loads, instead of clicking the Cancel button. To make the onCancel method works with the Cancel button, we could modify the code as below:

@{
    var action_name = "delete";
}

@(Html.Kendo().Dialog()
    .Name("dialog")
    .Title("Software Update")
    .Actions(actions => 
    {
        actions.Add().Text("OK");
        actions.Add().Text("Cancel").Action("function() { onCancel('"+ action_name +"') }"); // Wrap parameter in quotes
    })
)

<script type="text/javascript">
    function onCancel(action_name) {
        alert(action_name); // Will display 'delete'
    }
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.