Passing variable to Ajax.ActionLink OnSuccess, OnComplete, OnFailure, etc

青春壹個敷衍的年華 提交于 2019-12-13 14:21:29

问题


I'd like to put a div on my master page that I can update from anywhere in my site with updates. i.e. "recorded updated", "there was an error", etc.

I'm going to style the div differently depending on the type of update. "fail", "success", "info". Basic stuff so far.

So I have several ActionLinks throughout the site and they display their content fine in the updateTarget and I can even have them run fine when I pass OnComplete, OnBegin, etc. functions to them. However, I'd like to be able to send a parameter to that OnBegin function.

Example: .OnBegin="someFunction('fail');"

Any ideas on how to accomplish what I'm doing here?


回答1:


 <% string message = "failed"; %>
        <%=Ajax.ActionLink("TestController","TestAction",
new AjaxOptions{OnBegin="myFunction('" + message + "')"}) %>



回答2:


You can create an anonymous function, and next call your function with your paramas

<%: Ajax.ActionLink("Text", "ActionName", new { id = item.id }, new AjaxOptions { HttpMethod = "Post", OnComplete = "function(){myFunction(" + item.id.ToString() + ");}" })%>

The script:

    <script type="text/javascript">
    function onDelete(id) {
        alert("hello "+id);
    }
</script>


来源:https://stackoverflow.com/questions/1003901/passing-variable-to-ajax-actionlink-onsuccess-oncomplete-onfailure-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!