14

Im using dotnet core MVC and am having issues with asp-validation-summary.

Validation is working at a field level using asp-validation-for however I am not getting anything showing up in the asp-validation-summary.

<div asp-validation-summary="ModelOnly" class="text-danger"></div>

I have also tried

<div asp-validation-summary="All" class="text-danger"></div>

Any ideas what I am missing.

Done loads of searching and cant see solution to my problem

Thanks

10
  • 1
    Could you please include the Model & Controller code Commented Jun 15, 2017 at 9:31
  • I'm having the same problem and none of the answers here look like they solve it (hence I guess why none are accepted and all have zero votes). Did you ever find a solution for this? Commented Jul 24, 2020 at 14:10
  • We need you to show the view, as well as the generated HTML for the form. Without those, we can only guess. My guess could be that you have some styling that puts display: none !important; on any element that has class .validation-summary-errors. This is a valid guess for example for why validation is working at field levels but not at the summary level LOL. Commented Jul 28, 2020 at 1:33
  • 1
    @zola25: sure here you go: github.com/davidliang2008/DL.NetCore.EmptySolution. I just checked in some codes just to show that: github.com/davidliang2008/DL.NetCore.EmptySolution/commit/… Commented Jul 28, 2020 at 23:00
  • 1
    @zola25: I finally understand what's your problem is now. I have a simple solution for it: just copy <span asp-validation-for=""></span> and put them on top of the form. That way it shows the errors when a field is changed. Now you might want a more solid answer than this. If you could ask a separate question, I might be able to work on it and give you another answer. I don't think your problem is the same as this original asker had. Commented Jul 29, 2020 at 22:53

4 Answers 4

3

Also you can include jquery script for validation tag-helper

<form asp-controller="home" asp-action="CreatePoll" method="post" class="mt-3">
        <div class="validation" asp-validation-summary="All"></div>
        <div class="form-group">
            <label asp-for="Name">Name of the Poll:</label>
            <input asp-for="Name" class="form-control" />
            <span asp-validation-for="Name" class="text-danger"></span>
        </div>

            <button type="submit" class="btn btn-primary">Create</button>

    </form>

    @section scripts{

        <script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
        <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
    }
Sign up to request clarification or add additional context in comments.

Comments

2

I had the same issue, field validation worked well, can display xx is required below the input window. But the validation-summary didn't show up.

At first, I have code below in my cshtml file,

<div class="row">
    <div asp-validation-summary="ModelOnly"></div>
    @Html.ValidationSummary(false, "", new { @class = "text-danger" })
</div>

and I had code below in my controller,

ModelState.AddModelError("key", "validation summary error");
return View(rdl);

then only @Html.ValidationSummary() worked, and asp-validation-summary didn't show up(if it displayed message, it should have black font)

enter image description here

Then I tried asp-validation-summary="All", it worked. The difference between ALL and ModelOnly is ModelOnly will only display the invalid message for the model.

enter image description here

So I use ModelState.AddModelError("", "validation summary error."); in my Controller to make the <div asp-validation-summary="ModelOnly"></div> display error message.

I only know it can make it display correctly, but I don't know why...

Comments

0

Display field level error messages using ValidationSummary:

By default, ValidationSummary filters out field level error messages. If you want to display field level error messages as a summary then specify excludePropertyErrors = false.

Example: ValidationSummary to display field errors

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

So now, in the view will display error messages as a summary at the top. Please make sure that you don't have a ValidationMessageFor method for each of the fields.

official doc:https://msdn.microsoft.com/en-us/library/system.web.mvc.html.validationextensions.validationsummary(v=vs.118).aspx

Hope it will be helpful

Thanks

Karthik

1 Comment

This is the same as <div asp-validation-summary="All" class="text-danger"></div>...
0

You should add this for each your model field with asp-validation-summary="ModelOnly" and set @Html.ValidationSummary() before html form

//Validation Summary Parameters
public static MvcHtmlString ValidationSummary(
    this HtmlHelper htmlHelper,
    bool excludePropertyErrors, // to include or exclude your field validation errors
    string message,
    object htmlAttributes,
    string headingTag
)

@Html.ValidationSummary() // use this with your desired parameters
<form>
  ...... // your inputs
  <input asp-for="YourField" class="form-control" />
  <span asp-validation-for="YourField" class="text-danger"></span>
</form> 

1 Comment

I have, and have set message to be * but want the text to display in the summary

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.