简体   繁体   English

为什么我的验证摘要不显示?

[英]Why does my validation summary not render?

I have this viewmodel: 我有这个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: 以及标准.NET Core和EF Core程序包为其提供支持的Create视图的相关部分:

<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. 但是,当我省略必需的LastName ,我仅在该输入下收到一条验证消息,并且验证摘要完全不呈现。 In all other forms in my project, the summary is rendered as well as the message under the LastName input. 在我项目的所有其他形式中,摘要以及“ LastName输入下的消息均已呈现。 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. 我已经省略了客户端验证,即_ValidationScriptsPartial ,因为我正在测试模型绑定验证控制器端,并且添加了无关的自定义验证。 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. 在呈现的HTML中,您可以清楚地看到缺少的LastName产生了ModelState错误,而我一直给人的印象是,只要ModelState包含模型错误(即以输入名称作为键的模型错误),就可以采用这种形式验证摘要应呈现。 Why isn't it? 为什么不呢

In this particular case i think it is the way you add errors to the "ModelState" 在这种特殊情况下,我认为这是将错误添加到“ 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. 请注意, 是一个空字符串 ,这是“ ModelOnly”知道它不是属性错误的方式。

If you have anything other than a Empty string it will count as a Property Error, and will only show in the mode " ALL " 如果您有除Empty字符串以外的任何内容,它将被视为属性错误,并且只会以“ ALL ”模式显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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