gpt4 book ai didi

c# - 如何将带有 “required” html 属性的自定义错误消息添加到 mvc 5 razor View 文本输入编辑器

转载 作者:太空狗 更新时间:2023-10-29 17:27:50 25 4
gpt4 key购买 nike

我对 Asp.Net MVC 很天真。

我有一个部分 View (ASP.Net MVC),其中有一些必填字段 如果未提供任何必填字段,我想显示自定义错误消息。下面是我的局部 View 的完整 cshtml 代码。

@model CMSAdminPanel.ViewModel.ProductView
<h4>Material And Labour Cost For Each Size</h4>
<hr />
@Html.ValidationSummary(false, "", new { @class = "text-danger" })
@for (int i = 0; i < Model.ServiceView.ListPriceView.Count; i++)
{
@Html.HiddenFor(x => x.ServiceView.ListPriceView[i].ProductSizeType)
<div class="form-group">
@Html.LabelFor(x => x.ServiceView.ListPriceView[i].ProductSizeTypeName, "Size - " + Model.ServiceView.ListPriceView[i].ProductSizeTypeName, htmlAttributes: new { @class = "control-label col-md-4" })
</div>

<div class="form-group">
@Html.LabelFor(x => x.ServiceView.ListPriceView[i].LabourCost, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(x => x.ServiceView.ListPriceView[i].LabourCost, new { htmlAttributes = new { @class = "form-control", required = "required"} })
@Html.ValidationMessageFor(x => x.ServiceView.ListPriceView[i].LabourCost,"", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(x => x.ServiceView.ListPriceView[i].MaterialCost, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(x => x.ServiceView.ListPriceView[i].MaterialCost, new { htmlAttributes = new { @class = "form-control", required = "required" } })
@Html.ValidationMessageFor(x => x.ServiceView.ListPriceView[i].MaterialCost, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(x => x.ServiceView.ListPriceView[i].Profit, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(x => x.ServiceView.ListPriceView[i].Profit, new { htmlAttributes = new { @class = "form-control", required = "required" } })
@Html.ValidationMessageFor(x => x.ServiceView.ListPriceView[i].Profit, "", new { @class = "text-danger"})
</div>
</div>
}

我想在收到“需要此字段”时显示自定义消息“需要 Material 成本”。所以我想在客户端覆盖这个错误消息。

我想实现这样的目标:

<div class="form-group">
@Html.LabelFor(x => x.ServiceView.ListPriceView[i].LabourCost, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(x => x.ServiceView.ListPriceView[i].LabourCost, new { htmlAttributes = new { @class = "form-control", required = "required", **data_val_required = "LabourCost is requried"**} })
@Html.ValidationMessageFor(x => x.ServiceView.ListPriceView[i].LabourCost,"", new { @class = "text-danger" })
</div>
</div>

任何建议/解决方案都会有很大帮助

最佳答案

在您的模型类中,将 [Required] 属性添加更改为

[Required(ErrorMessage = "Material cost is required")]
public decimal MaterialCost {get;set;}

另一种方法是使用 JQuery 从 JavaScript 设置它或覆盖设置它的属性。默认情况下 ValidationMessageFor 的输出是

data-val-required="The field is required.".

因此,您可以在标记中覆盖此值

关于c# - 如何将带有 “required” html 属性的自定义错误消息添加到 mvc 5 razor View 文本输入编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41485898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com