Ajax.BeginForm doesn't call onSuccess

后端 未结 4 946
傲寒
傲寒 2021-01-05 20:01

In ASP.NET MVC 3 application I use Ajax.BeginForm to post writed text to controller.

@using (Ajax.BeginForm(\"Post\", \"Forum\", new {threadId = Model.Thread         


        
相关标签:
4条回答
  • 2021-01-05 20:42

    Thanks to "Darin Dimitrov" and his answer for pointing out a lot about this. But just adding to it, I know this might be simple but this got me,

    My mistake was that I thought

    jquery.validate.unobtrusive.js
    

    and

     jquery.unobtrusive-ajax.js
    

    where the same when they aren't. Make sure you use the "jquery.unobtrusive-ajax.js" found in Nuget.

    Microsoft.jQuery.Unobtrusive.Ajax

    0 讨论(0)
  • 2021-01-05 20:46

    here the best solution i have found 100% fix..

    http://www.learnsharecorner.com/asp-net/ajax-begin-form-onsuccess-is-not-working-asp-net-mvc/

    0 讨论(0)
  • 2021-01-05 20:49

    It is a much nicer solution when you give your form an Id and then attach the submit event in a separate javascript file (using jQuery) and handle the post there

    html:

    @using (Ajax.BeginForm("Post", "Forum", new {threadId = Model.Thread.Id  }, new {id = "formid" }))
    {
     ...
    

    Javascript:

    $("#formid").submit(function (e) {
        e.preventDefault();
        if ($(this).valid()) {
            $.ajax({
                type: "POST",
                url: $(this).attr('action'),
                data: $(this).serialize(),
                success: function (result) {
                    //do your success logic
                }
            });
        }
    });
    
    0 讨论(0)
  • 2021-01-05 20:50

    Make sure you have included the following script to your page:

    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
    

    and that you have enabled unobtrusive javascript in your web.config:

    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    

    This is what makes Ajax.* helpers such as Ajax.BeginForm to work.

    0 讨论(0)
提交回复
热议问题