如何使用“required”htmlattribute向mvc 5 razor视图文本输入编辑器添加自定义错误消息
我对Asp.Net MVC很天真。
我有一个局部视图(ASP.Net MVC),其中我有一些必填字段我想显示自定义错误消息,如果没有提供任何必填字段。 以下是我的局部视图的完整cshtml代码。
@model CMSAdminPanel.ViewModel.ProductView Material And Labour Cost For Each Size
@Html.ValidationSummary(false, "", new { @class = "text-danger" }) @for (int i = 0; i x.ServiceView.ListPriceView[i].ProductSizeType) @Html.LabelFor(x => x.ServiceView.ListPriceView[i].ProductSizeTypeName, "Size - " + Model.ServiceView.ListPriceView[i].ProductSizeTypeName, htmlAttributes: new { @class = "control-label col-md-4" }) @Html.LabelFor(x => x.ServiceView.ListPriceView[i].LabourCost, htmlAttributes: new { @class = "control-label col-md-2" }) @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" }) @Html.LabelFor(x => x.ServiceView.ListPriceView[i].MaterialCost, htmlAttributes: new { @class = "control-label col-md-2" }) @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" }) @Html.LabelFor(x => x.ServiceView.ListPriceView[i].Profit, htmlAttributes: new { @class = "control-label col-md-2" }) @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"}) }
我想显示自定义消息“物料成本是必需的”,而我得到“此字段是必需的”。 所以我想在客户端覆盖此difault错误消息。
我希望实现这样的目标:
@Html.LabelFor(x => x.ServiceView.ListPriceView[i].LabourCost, htmlAttributes: new { @class = "control-label col-md-2" }) @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" })
任何建议/解决方案都会有很大的帮助
在模型类中,添加将[Required]属性更改为
[Required(ErrorMessage = "Material cost is required")] public decimal MaterialCost {get;set;}
另一种方法是使用JQuery从JavaScript设置它或覆盖设置它的属性。 默认情况下, ValidationMessageFor
的输出是
data-val-required="The field is required.".
因此,您可以在标记中覆盖此值
我找到了一种方法来使用htmlAttribute title属性覆盖客户端上的默认必需消息,下面是代码:
@Html.LabelFor(x => x.ServiceView.ListPriceView[i].LabourCost, htmlAttributes: new { @class = "control-label col-md-2" }) @Html.EditorFor(x => x.ServiceView.ListPriceView[i].LabourCost, new { htmlAttributes = new { @class = "form-control", required = "required", title = "LabourCost is requried"} }) @Html.ValidationMessageFor(x => x.ServiceView.ListPriceView[i].LabourCost,"", new { @class = "text-danger" })
在你的模型中
[Required(ErrorMessage = "Material cost is required")] public doubleMaterialCost { get; set; }
如果您的站点中有多种文化,则可以选择从资源加载它并传递资源字符串。
或者在你的行动中
上述就是C#学习教程:如何使用“required”htmlattribute向mvc 5 razor视图文本输入编辑器添加自定义错误消息分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
public ActionResult(YourModel model) { if (model.doubleMaterialCost == 0) ModelState.AddModelError("doubleMaterialCost ", "Material cost is required");
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/989143.html