How can I pass parameters to the OnSuccess function of the AjaxOptions class in ASP.NET MVC?
How can I pass parameters to the OnSuccess
function of the AjaxOptions
class in ASP.NET MVC?
Here's my code but it doesn't work:
<%= Ajax.ActionLink("Delete",
"Delete",
"MyController",
New With {.id = record.ID},
New AjaxOptions With
{
.Confirm = "Delete record?",
.HttpMethod = "Delete",
.OnSucce开发者_开发问答ss = "updateCount('parameter')"
})
%>
UPDATE
Setting the OnSuccess
property to (function(){updateCount('parameter');})
solved my problem:
<%= Ajax.ActionLink("Delete",
"Delete",
"MyController",
New With {.id = record.ID},
New AjaxOptions With
{
.Confirm = "Delete record?",
.HttpMethod = "Delete",
.OnSuccess = "(function(){updateCount('parameter');})"
})
%>
You should be able to use a jQuery selector to populate a value from a field in the page:
<%= Ajax.ActionLink("Delete",
"Delete",
"MyController",
New With {.id = record.ID},
New AjaxOptions With
{
.Confirm = "Delete record?",
.HttpMethod = "Delete",
.OnSuccess = "updateCount($('#SomeField).val()))"
})
%>
Also take a look here: Can I pass a parameter with the OnSuccess event in a Ajax.ActionLink
Here is a MVC4 example. OnBegin, OnSuccess, OnComplete and OnFailure -functions are used to enable/disable my ajax animations. Each function passes a item Id as parameter to allow me to reuse my js functions for all my ajax sections. ajaxOnbegin() shows a gif and ajaxOnsuccess hides it again.
<script>
@*Ajax Animation*@
$(document).ready(function () {
$("#ajaxLoadingGif").hide();
});
function ajaxOnbegin(id) {
//show animated gif
$(id).show();
}
function ajaxOnsuccess(id) {
//disable animated gif
$(id).hide();
}
function ajaxOnfailure(id) {
//disbale animated gif
$(id).hide();
}
function ajaxOncomplete(id) {
//disable animated gif
$(id).hide();
}
</script>
@Ajax.ActionLink(linkText: " Hi", // <-- Text to display
actionName: "getJobCards", // <-- Action Method Name
routeValues: new { searchString = ViewBag.searchString},
ajaxOptions: new AjaxOptions{
"#itemId", // <-- DOM element ID to update
InsertionMode = InsertionMode.Replace,
HttpMethod = "GET", // <-- HTTP method
OnBegin = "ajaxOnbegin('#ajaxLoadingGif')",
//="ajaxOnbegin" without parameters
OnSuccess = "ajaxOnsuccess('#ajaxLoadingGif')",
OnComplete = "ajaxOncomplete('#ajaxLoadingGif')",
OnFailure = "ajaxOnfailure('#ajaxLoadingGif')"
},
htmlAttributes: new { id = ViewBag.ajaxId }
)
精彩评论