在我的一个控制器操作中,我所做的第一件事就是将模型传递给一个新的操作,它实际上只是解析输入以确定用户是否输入了一个有效日期。然后返回模型并检查ModelState.IsValid。
public Import ValidateUploadModel(Import Model)
{
// DO not allow future dates
if (Model.CurrMoInfo.CurrMo > DateTime.Now)
{
ModelState.AddModelError("FutureDate", "You cannot assign a future date.");
}
//Do not allow dates from the same month (we run the processing a month behind)
if (Model.CurrMoInfo.CurrMo.Month == DateTime.Now.Month)
{
ModelState.AddModelError("SameMonth", "You must process a previous month.");
}
//Ensure day is last day of a previous month
if (Model.CurrMoInfo.CurrMo.Day != DateTime.DaysInMonth(Model.CurrMoInfo.CurrMo.Year, Model.CurrMoInfo.CurrMo.Month))
{
ModelState.AddModelError("LastDay", "You must enter the last day of the month.");
}
//Do not allow dates older than 12 months back
if (Model.CurrMoInfo.CurrMo < DateTime.Now.AddMonths(-12))
{
ModelState.AddModelError("TooOld", "Date must not be older than a year.");
}
return Model;
}在我知道我有模型状态错误的时候,我可以在我的剃刀视图中正确地显示它们,方法是
<span class="text-danger">@Html.ValidationSummary(false)</span>因此,由于我的所有模型状态错误都是针对页面上相同的输入的,所以我可以安全地做到这一点。但是,如果我有各种不同的输入,需要独立地显示不同的错误,那么又该怎么办呢?我该怎么做呢?此外,除了使用@Html.ValidationSummary?之外,还有更好的(或更合适的)方法来做到这一点吗?
我已经搜索过微软的文档和几十个StackOverflow问题,试图把旧的答案翻译成.Net核心的方法,而不是运气。
清晰的编辑:
这是剃须刀视图中的整张卡片:
<div class="card-body">
@if (Model.StagingCount == 0)
{
<input asp-for="@Model.CurrMoInfo.CurrMo" type="date" required class="col-lg-12" />
}
else
{
<input asp-for="@Model.CurrMoInfo.CurrMo" type="date" disabled="disabled" required class="col-lg-12" />
}
<span class="text-danger">@Html.ValidationSummary(false)</span>
</div>输入是模型属性的输入,但是没有注释。我已经编写了自己的规则,并在没有遵守规则的情况下手动将错误添加到模型状态。但是,当我开始需要验证更多字段时,代码是不可伸缩的。我只想知道有什么更好的方法。
发布于 2019-06-05 14:05:13
在上面的例子中,您并没有真正遵循标准实践。
对于像这样的简单验证(其中只在一个字段中验证值),您用来在ModelState中放置错误消息的键应该与模型中受影响字段的名称相同。在您的情况下,您的所有错误都应该记录在CurrMoInfo.CurrMo键上。只有错误消息本身需要不同。据我所知,对每个特定的不同错误使用自定义键不会给应用程序带来任何价值。你并没有按原来的方式使用它。
如果您将它们全部记录在CurrMoInfo.CurrMo上,那么您可以使用asp-validation-for标记助手来创建一个字段,该字段专门显示该字段的错误。
<span asp-validation-for="CurrMoInfo.CurrMo" class="text-danger"></span>然后(可选)使用ValidationSummary (如标题所示)总结整个模型的所有错误,并显示可能创建的与单个特定字段无关的任何额外模型错误。
完整的例子:
public Import ValidateUploadModel(Import Model)
{
// DO not allow future dates
if (Model.CurrMoInfo.CurrMo > DateTime.Now)
{
ModelState.AddModelError("CurrMoInfo.CurrMo", "You cannot assign a future date.");
}
//Do not allow dates from the same month (we run the processing a month behind)
if (Model.CurrMoInfo.CurrMo.Month == DateTime.Now.Month)
{
ModelState.AddModelError("CurrMoInfo.CurrMo", "You must process a previous month.");
}
//Ensure day is last day of a previous month
if (Model.CurrMoInfo.CurrMo.Day != DateTime.DaysInMonth(Model.CurrMoInfo.CurrMo.Year, Model.CurrMoInfo.CurrMo.Month))
{
ModelState.AddModelError("CurrMoInfo.CurrMo", "You must enter the last day of the month.");
}
//Do not allow dates older than 12 months back
if (Model.CurrMoInfo.CurrMo < DateTime.Now.AddMonths(-12))
{
ModelState.AddModelError("CurrMoInfo.CurrMo", "Date must not be older than a year.");
}
return Model;
}
<div class="card-body">
@if (Model.StagingCount == 0)
{
<input asp-for="CurrMoInfo.CurrMo" type="date" required class="col-lg-12" />
}
else
{
<input asp-for="CurrMoInfo.CurrMo" type="date" disabled="disabled" required class="col-lg-12" />
}
<span asp-validation-for="CurrMoInfo.CurrMo" class="text-danger"></span>
</div>进一步阅读:https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/validation?view=aspnetcore-2.2
我不认为这个一般原则已经从.NET框架转变为.NET核心。
https://stackoverflow.com/questions/56461726
复制相似问题