RJP
RJP

Reputation: 4116

MVC Ajax.BeginForm not showing validation errors

Here is my controller action:

[HttpPost]
        public ActionResult CreateModal(MyModel myModel)
        {
            if (ModelState.IsValid)
            {
              //success logic
            }
            return PartialView("_MyPartial", myModel);

        }

And my partial(it's in a jQuery dialog):

@using (Ajax.BeginForm("CreateModal", "MyController", new {area = "MyArea2"}, new AjaxOptions() {HttpMethod = "POST", InsertionMode = InsertionMode.Replace}))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "", new {@class = "text-danger"})
        <div class="row">
            <fieldset>
                <legend>Info</legend>
                <div class="row">
                    <div class="form-group col-md-4">
                        @Html.LabelFor(model => model.Name, htmlAttributes: new {@class = "control-label"})
                        @Html.EditorFor(model => model.Name, new {htmlAttributes = new {@class = "form-control"}})
                        @Html.ValidationMessageFor(model => model.Name, "", new {@class = "text-danger"})
                    </div>
                </div>
                </fieldset>
                </div>
                }

I can post to my action just fine, but if my ModelState is not valid, I get no validation errors on return PartialView("_MyPartial", myModel), even though stepping through the debugger I'm seeing the errors.

I'm using the latest versions of jquery and related tech from nuget.

Also, not sure if it matters, but I am posting from MyArea1 to a controller in MyArea2. The _MyPartial lives in a common folder.

Upvotes: 1

Views: 3020

Answers (1)

Mohammad Farazmaan
Mohammad Farazmaan

Reputation: 11

AjaxOptions(){
    ...
    OnComplete = "ParsUnob"
    ...
}

Script Code:

function ParsUnob() {
    $.validator.unobtrusive.parse("form");
}

Upvotes: 1

Related Questions