1

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.

4
  • 1
    You have not specified an UpdateTargetId in the AjaxOptions (and why do you not implement unobtrusive client side validation to prevent the form submitting if its invalid?) Commented Aug 2, 2016 at 5:40
  • I was under the impressions I don't need UpdateTargetId if I am returning the entire partial view. Commented Aug 2, 2016 at 5:43
  • @StephenMuecke how to implement unobtrusive client side validation? Commented Aug 2, 2016 at 5:47
  • 1
    Include jquery.validate.js and jquery.validate.unobtrusive.js in your view or layout, or better, include the bundle ( "~/bundles/jqueryval") Commented Aug 2, 2016 at 5:49

1 Answer 1

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

Script Code:

function ParsUnob() {
    $.validator.unobtrusive.parse("form");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.