C# 如何使用“;必需的”;htmlattribute to mvc 5 razor视图文本输入编辑器

C# 如何使用“;必需的”;htmlattribute to mvc 5 razor视图文本输入编辑器,c#,asp.net,asp.net-mvc,razor,C#,Asp.net,Asp.net Mvc,Razor,我对Asp.NETMVC很幼稚 我有一个局部视图(ASP.Net MVC),其中有一些必填字段,如果未提供其中任何必填字段,我希望显示自定义错误消息。下面是我的部分视图的完整cshtml代码 @model CMSAdminPanel.ViewModel.ProductView <h4>Material And Labour Cost For Each Size</h4> <hr /> @Html.ValidationSummary(false, "", new

我对Asp.NETMVC很幼稚

我有一个局部视图(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>
}
@model CMSAdminPanel.ViewModel.ProductView
每种尺寸的材料和人工成本

@Html.ValidationSummary(false,“,new{@class=“text danger”}) @对于(int i=0;ix.ServiceView.ListPriceView[i].ProductSizeType) @LabelFor(x=>x.ServiceView.ListPriceView[i].ProductSizeTypeName,“大小-”+Model.ServiceView.ListPriceView[i].ProductSizeTypeName,htmlAttributes:new{@class=“control label col-md-4”}) @LabelFor(x=>x.ServiceView.ListPriceView[i].LabourCost,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(x=>x.ServiceView.ListPriceView[i].LabourCost,new{htmlAttributes=new{@class=“form control”,required=“required”}) @Html.ValidationMessageFor(x=>x.ServiceView.ListPriceView[i].LabourCost,“,新{@class=“text danger”}) @LabelFor(x=>x.ServiceView.ListPriceView[i].MaterialCost,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(x=>x.ServiceView.ListPriceView[i].MaterialCost,new{htmlAttributes=new{@class=“form control”,required=“required”}) @Html.ValidationMessageFor(x=>x.ServiceView.ListPriceView[i].MaterialCost,“,新{@class=“text danger”}) @LabelFor(x=>x.ServiceView.ListPriceView[i]。利润,htmlAttributes:new{@class=“control label col-md-2”}) @EditorFor(x=>x.ServiceView.ListPriceView[i]。利润,新的{htmlAttributes=new{@class=“form control”,required=“required”}) @Html.ValidationMessageFor(x=>x.ServiceView.ListPriceView[i].Profile,“,new{@class=“text danger”}) }
当我得到“此字段为必填字段”时,我想显示自定义消息“需要材料成本”。所以我想在客户端重写这个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>

@LabelFor(x=>x.ServiceView.ListPriceView[i].LabourCost,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(x=>x.ServiceView.ListPriceView[i].LabourCost,新的{htmlAttributes=new{@class=“form control”,required=“required”,**data\u val\u required=“LabourCost需要”**})
@Html.ValidationMessageFor(x=>x.ServiceView.ListPriceView[i].LabourCost,“,新{@class=“text danger”})
任何建议/解决方案都会对您的模型大有帮助

[Required(ErrorMessage = "Material cost is required")]
public doubleMaterialCost { get; set; }
如果站点中有多个区域性,您可以选择从资源加载它并传递资源字符串

还是在你的行动中

public ActionResult(YourModel model)
{
    if (model.doubleMaterialCost == 0)
            ModelState.AddModelError("doubleMaterialCost ", "Material cost is required");

在模型类中,将[Required]属性添加到

[Required(ErrorMessage = "Material cost is required")]
public decimal MaterialCost {get;set;}
另一种方法是使用JQuery从JavaScript设置它或重写设置它的属性。默认情况下,
ValidationMessageFor
的输出为

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

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

我找到了一种在客户端使用htmlAttributetitle属性覆盖此默认必需消息的方法,下面是代码:

<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>

@LabelFor(x=>x.ServiceView.ListPriceView[i].LabourCost,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(x=>x.ServiceView.ListPriceView[i].LabourCost,新的{htmlAttributes=new{@class=“form control”,required=“required”,title=“LabourCost需要”})
@Html.ValidationMessageFor(x=>x.ServiceView.ListPriceView[i].LabourCost,“,新{@class=“text danger”})

您是否尝试在模型中的MaterialCost上使用数据注释?从数据注释中添加所需的属性,并在错误消息中传递消息parameter@NoorSamara是的,首先我尝试在我的模型上对所需的属性和消息进行数据注释,但对于局部视图,模型注释不起作用。所以我尝试了HTMLattribute“required”,它工作正常,但是它显示了错误消息“this field is required”。我自己做了一个测试,自定义的required消息工作正常,使用以下属性:
public decimal MaterialCost{..}{..}{..}
,无论是从视图还是从局部视图触发。这是在MVC v5.2.3中实现的。您是否也在使用它,或者可能是一个旧版本?@PeterB是的,这两种情况都可以,但我希望不使用“ModelState.IsValid”进行客户端验证和模型中的必需属性。请检查以下链接:我已经在模型中使用了此注释,但如果部分视图不起作用。@AashishKumar您可以分享您的控制器操作吗。@AashishKumar我还使用另一种验证方式编辑我的答案,你们也可以这样尝试。实际上我想在客户端做一些事情,而不是在控制器级别。是否可能?您能告诉我如何将ValidationMessageFor的默认输出改写为“需要材料成本”吗。提前谢谢。您可以在视图中使用的模型类中重写它,只需在我的答案中添加一行。这是一个局部视图,所以我只想执行客户端验证,而不是“ModelState.IsValid”在我的控制器中。Html助手创建客户端验证和服务器端验证,然后使用我在回答中解释的另一种方式,将数据val required=“字段为required.”。如果我有
编辑器for
type=“date”
type=“number”,这只对文本输入有效
它给出了它的