1

I am using MVC3 + Razor. Now I have a form which contains Ajax.BeginForm(...). I following exact what "Maxim" said in this post:

ASP.NET MVC AJAX with HTML.ValidationMessageFor

And my validation rule is defined like:

ModelState.AddModelError("ControlName", "error message");

However my

@Html.ValidationMessageFor(model => Model.OneProperty[0].AnotherProperty) 

didn't give me anything.

I also put these code into partial-view for "error summary" as well. I could see code run through the "ValidationSummary" PV, but it just doesn't display.

 if (!ViewData.ModelState.IsValid)
 {
    <div id="errorMessage" class="notificationArea">
             @Html.Partial("ValidationSummary")
    </div>
  }

In short, my main view is this:

@using (Ajax.BeginForm("SaveFromMainView", null, new AjaxOptions
{
    HttpMethod = "POST",
    UpdateTargetId = "myForm"   
}, 
    new { id = "myForm" }))
{           

    @Html.Partial("SamTest1")
}

And within this "SamTest1" partial-view, there is another "ValidationSummary" partial-view to display bullets of error message.

And my action method fianlly:

return PartialView("SamTest1"); 

Thanks heaps in advance!

1
  • Have you tried using Html.ValidationSummary? Also, make sure that you have enabled clientvalidation and added in the unobtrusive libraries. Finally, any valiationmessages will only display if the control name you add to the modelstate matches the name of one of the validationmessagefor controls. Commented Nov 3, 2012 at 13:22

1 Answer 1

1

I got it work. Actually it's due to incorrect bind of model. I have a list of model object, and had though something like ControlFor(model => Model[0].PropertyName) ... would work. Actually it's not.

After changing it to normal model binding, the validation message shows now.

Thanks again all!

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.