Aas*_*mar 9 c# asp.net asp.net-mvc razor
我对Asp.Net MVC很天真.
我有一个局部视图(ASP.Net MVC),其中我有一些必需的字段我想显示自定义错误消息,如果没有提供任何必填字段.以下是我的部分视图的完整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>
}
Run Code Online (Sandbox Code Playgroud)
我想显示自定义消息"需要材料成本",而我收到"此字段是必需的".所以我想在客户端覆盖此difault错误消息.
我希望实现这样的目标:
<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>
Run Code Online (Sandbox Code Playgroud)
任何建议/解决方案都会有很大的帮助
Hai*_*dad 30
在模型类中,添加更改[Required]属性
[Required(ErrorMessage = "Material cost is required")]
public decimal MaterialCost {get;set;}
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用JQuery从JavaScript设置它或覆盖设置它的属性.默认情况下输出ValidationMessageFor为
data-val-required="The field is required.".
Run Code Online (Sandbox Code Playgroud)
因此,您可以在标记中覆盖此值
我找到了一种方法来使用htmlAttribute title属性覆盖客户端上的默认必需消息,下面是代码:
<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", title = "LabourCost is requried"} })
@Html.ValidationMessageFor(x => x.ServiceView.ListPriceView[i].LabourCost,"", new { @class = "text-danger" })
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
在你的模型中
[Required(ErrorMessage = "Material cost is required")]
public doubleMaterialCost { get; set; }
Run Code Online (Sandbox Code Playgroud)
如果您的站点中有多种文化,您可以选择从资源加载它并传递资源字符串。
或者在你的行动中
public ActionResult(YourModel model)
{
if (model.doubleMaterialCost == 0)
ModelState.AddModelError("doubleMaterialCost ", "Material cost is required");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35712 次 |
| 最近记录: |