c# - How to add custom error message with “required” htmlattribute to mvc 5 razor view text input editor -
i naive asp.net mvc.
i have partial view(asp.net mvc) in have required fields want show custom error message if of required field in not provided. below complete cshtml code partial view.
@model cmsadminpanel.viewmodel.productview <h4>material , labour cost each size</h4> <hr /> @html.validationsummary(false, "", new { @class = "text-danger" }) @for (int = 0; < 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> }
i want show custom message "material cost required" while getting "this field required". want override difault error message on client side.
i want achieve this:
<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 requried"**} }) @html.validationmessagefor(x => x.serviceview.listpriceview[i].labourcost,"", new { @class = "text-danger" }) </div> </div>
any suggestion/solution great help
in model class, add change [required] attribute
[required(errormessage = "material cost required")] public decimal materialcost {get;set;}
another approach set javascript using jquery or override attribute sets it. default output of validationmessagefor
data-val-required="the field required.".
so, can override value in markup
Comments
Post a Comment