Mvc 4 Summary Check Errors Not Displaying

I am having trouble displaying the validation summary, I only want to display errors in the validation summary, and not next to the field. In my opinion, I,

@Html.ValidationSummary() @Html.ValidationMessageFor(m =>m.Name) 

in my controller i have

 ModelState.AddModelError("Name", "name is required"); 

Shouldn't I get a validation error message? on the top? I don’t understand what is missing ...

I also include these script files. jquery.validate.min.js jquery.validate.unobtrusive.min.js

+6
source share
4 answers

Try

 @Html.ValidationSummary(false) 

so that he does not rule out a property error.

OR

Try the associated @xurca method, which should add a model error with a blank key, so it is not tied to a specific property.

 ModelState.AddModelError(String.Empty, "name is required"); 
+26
source

If you selected your field as

  @Html.TextBoxFor(model => model.Name, new { id = "staffSearchFilterName", value = "", **Name = "staffSearchFilterName"** }) 

then you should use

 @Html.ValidationMessage("staffSearchFilterName") 
+2
source

If your call to @ Html.ValidationSummary () is in partial view, DO NOT pass the data in partial form as follows:

 @Html.Partial("_YourForm", Model, new ViewDataDictionary { { "Submit", true }}) 

Instead, first add a pair of key values ​​to the Html.ViewData collection:

 @{ Html.ViewData.Add(new KeyValuePair<string,object>("Submit",true)); } 

then call the partial view:

 @Html.Partial("_YourForm", Model, Html.ViewData) 

this will allow ModelState to correctly propagate to a partial view.

0
source

In user controller error

  ModelState.AddModelError("test","test"); 

Summary:

  @Html.ValidationSummary(false, "", new { @class = "text-danger" }) 

Individual field:

  @Html.TextBoxFor(m => m.FieldName, new { @class= "form-control" }) @Html.ValidationMessageFor(m => m.FieldName, "", new { @class = "text-danger" }) 
-1
source

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


All Articles