Using Ajax.BeginForm with ASP.NET MVC 3 Razor

后端 未结 8 1028
面向向阳花
面向向阳花 2020-11-22 01:23

Is there a tutorial or code example of using Ajax.BeginForm within Asp.net MVC 3 where unobtrusive validation and Ajax exist?

This is an elusive topic f

8条回答
  •  臣服心动
    2020-11-22 01:32

    Darin Dimitrov's solution worked for me with one exception. When I submitted the partial view with (intentional) validation errors, I ended up with duplicate forms being returned in the dialog:

    enter image description here

    To fix this I had to wrap the Html.BeginForm in a div:

    @using (Html.BeginForm("CreateDialog", "SupportClass1", FormMethod.Post, new { @class = "form-horizontal" })) { //form contents }

    When the form was submitted, I cleared the div in the success function and output the validated form:

        $('form').submit(function () {
            if ($(this).valid()) {
                $.ajax({
                    url: this.action,
                    type: this.method,
                    data: $(this).serialize(),
                    success: function (result) {
                        $('#myForm').html('');
                        $('#result').html(result);
                    }
                });
            }
            return false;
        });
    });
    

提交回复
热议问题