简体   繁体   中英

Why does my validation summary not render?

I have this viewmodel:

public class PersonBindingModel
{
    public int Id { get; set; }
    [Required]
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public int Age { get; set; }
}

and a relevant portion of the Create view scaffolded for it by standard .NET Core and EF Core packages:

<form asp-action="Create">
    <div class="form-horizontal">
        <h4>PersonBindingModel</h4>
        <hr />
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="LastName" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="LastName" class="form-control" />
                <span asp-validation-for="LastName" class="text-danger"></span>
            </div>
        </div>
    </div>
    ...
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</form>

Yet when I omit the required LastName , I only get a validation message under that input, and the validation summary is not rendered at all. In all other forms in my project, the summary is rendered as well as the message under the LastName input. This is the rendered form:

<form action="/persons/Create" method="post">
    <div class="form-horizontal">
        <h4>PersonBindingModel</h4>
        <hr>

        <div class="form-group">
            <label class="col-md-2 control-label" for="LastName">LastName</label>
            <div class="col-md-10">
                <input class="form-control input-validation-error" type="text" data-val="true" data-val-required="The LastName field is required." id="LastName" name="LastName" value="">
                <span class="text-danger field-validation-error" data-valmsg-for="LastName" data-valmsg-replace="true">The LastName field is required.</span>
            </div>
        </div>
        ...
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default">
            </div>
        </div>
    </div>
    ...
</form>

I have omitted the client side validation, ie the _ValidationScriptsPartial , as I am testing the model binding validation controller side, together with an irrelevant custom validation I have added. The form renders just the same with or without the custom validation.

In the rendered HTML, you can clearly see the missing LastName has generated a ModelState error, and I have always been under the impression that in a form like this, as long as ModelState contains model errors, ie one's with input names as keys, the validation summary should render. Why isn't it?

In this particular case i think it is the way you add errors to the "ModelState"

ModelState.AddModelError(string.Empty, x.Description)

Note that the Key is a Empty string , This is how the "ModelOnly" knows that it is not a Property Error.

If you have anything other than a Empty string it will count as a Property Error, and will only show in the mode " ALL "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM