Partial Views & ModelState.AddModelError

LoginRegister view source is as follows:

 @Html.Partial("authentication/_login") @Html.Partial("authentication/_register") 

and each child view has a form with this syntax

 @using (Html.BeginForm(**seperated-methods**, "Login")) { @Html.ValidationSummary(false) } 

I am sending errors (s) in postback whit this code

 ModelState.AddModelError("", "**any-error-message**"); return View("authentication/LoginRegister", customized-data); 

Point, error message is displayed as in partial views .

+4
source share
1 answer

You need to tell ModelState which property this error belongs to:

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

Now it will be only in

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

If you do not specify a property name, the error will be considered global and will be displayed in each ValidationSummary .

+3
source

Source: https://habr.com/ru/post/1416236/


All Articles