7

I am having trouble displaying validation summary, I only want to display errors in validation summary, not beside the field. In my view i have,

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

in my controller i have

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

Am i not supposed to get a validation error message? at the top? I don't get what i am missing...

I am also including these script files.. jquery.validate.min.js jquery.validate.unobtrusive.min.js

9
  • 1
    see this question stackoverflow.com/questions/2818219/… Commented Aug 15, 2013 at 17:58
  • I tried that solution before i posted this question. I don't know what i am missing Commented Aug 15, 2013 at 18:29
  • Hmm, I don't think they are conflicting, but you don't need @Html.ValidationMessageFor(m =>m.Name) if you just want the summary. Commented Aug 15, 2013 at 18:31
  • Also have you tried looking at the actual source returned from the server to see if the error message is there? It is possible some CSS is causing it not to appear. Commented Aug 15, 2013 at 18:32
  • I looked in the css, the div tag is there but no text in the validation....I don't know what i am missing Commented Aug 15, 2013 at 19:00

3 Answers 3

30

Try

@Html.ValidationSummary(false)

so that it will not exclude property errors.

OR

Try the method @xurca linked which is to add the model error with an empty key so it is not tied to a specific property.

ModelState.AddModelError(String.Empty, "name is required");
Sign up to request clarification or add additional context in comments.

1 Comment

I'm working on two projects at the same time, VS '12 and VS '13. At any rate, I had no issues in '13 with the default template, but setting TRUE to FALSE solved it for me in the '12 project (as per your post). At any rate, thanks for the post.
2

If you custom named your field like

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

then you have to use

@Html.ValidationMessage("staffSearchFilterName")

Comments

2

If your @Html.ValidationSummary() call is in a Partial view, DON'T pass data into the partial view like this:

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

Instead, add the key value pair to Html.ViewData collection first:

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

then call the partial view:

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

this will allow the ModelState to propagate to the partial view properly.

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.