Ajax.BeginForm doesn't call onSuccess

后端 未结 4 971
傲寒
傲寒 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: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
                }
            });
        }
    });
    

提交回复
热议问题