Partial Views & ModelState.AddModelError

asp.net-mvc, asp.net-mvc-3, modelstate, partial-views

Solution

You need to tell the ModelState to which property this error refer to:

ModelState.AddModelError("PropertyName", "**any-error-message**");

Now it will be only in the

@Html.ValidationMessageFor(m => m.PropertyName)

If you don't specify the property name, the error will be considered global and get shown in every `ValidationSummary`.

Problem

the source of `LoginRegister` view is like this : ``` @Html.Partial("authentication/_login") @Html.Partial("authentication/_register") ``` and each child view has got a form with this syntax ``` @using (Html.BeginForm(**seperated-methods**, "Login")) { @Html.ValidationSummary(false) } ``` I send error(s) in postback whit this code ``` ModelState.AddModelError("", "**any-error-message**"); return View("authentication/LoginRegister", customized-data); ``` The point is , error message shows in both `partial views`.

Original source