What I am trying to achieve is to replace the default error message with a database driven error message. I created this same method to make all of my text, labels, static text, etc dynamic and it all works great. When I try to use the .Data() or .HtmlAttributes() it will not overwrite the data-val- objects. Model["validation.name.required"] translates to "Name is required." instead of "The Name field is required."
You can recreate it this way:
This works using standard TextBoxFor -
Html.TextBoxFor(x => x.Name, new { data_val_required = Model["validation.name.required"], @class="form-control" }), Html.ValidationMessageFor(x => x.Name)
Output:
<input name="Name" class="form-control" id="Name" type="text" value="" data-val-required="Name is required." data-val="true" data-val-length-max="50" data-val-length="The field Name must be a string with a maximum length of 50.">
This does not work -
@:@form.FormGroup().TextBoxFor(x => x.Name).Data(new { val_required = Model["validation.Name.required"]}).Placeholder(Model["placeholder.Name"]).ShowValidationMessage(true).Label().LabelText(Model["label.Name"])
Nor does this -
@:@form.FormGroup().TextBoxFor(x => x.Name).HtmlAttributes(new { data_val_required = Model["validation.Name.required"]}).Placeholder(Model["placeholder.Name"]).ShowValidationMessage(true).Label().LabelText(Model["label.Name"])
Output for both that do not work is the same:
<input name="Name" class="form-control input-validation-error" id="Name" type="text" placeholder="Enter your name, please" value="" data-val-required="The Name field is required." data-val="true" data-val-length-max="50" data-val-length="The field Name must be a string with a maximum length of 50.">
What I am trying to achieve is to replace the default error message with a database driven error message. I created this same method to make all of my text, labels, static text, etc dynamic and it all works great. When I try to use the .Data() or .HtmlAttributes() it will not overwrite the data-val- objects. Model["validation.name.required"] translates to "Name is required." instead of "The Name field is required."
You can recreate it this way:
This works using standard TextBoxFor -
Html.TextBoxFor(x => x.Name, new { data_val_required = Model["validation.name.required"], @class="form-control" }), Html.ValidationMessageFor(x => x.Name)
Output:
<input name="Name" class="form-control" id="Name" type="text" value="" data-val-required="Name is required." data-val="true" data-val-length-max="50" data-val-length="The field Name must be a string with a maximum length of 50.">
This does not work -
@:@form.FormGroup().TextBoxFor(x => x.Name).Data(new { val_required = Model["validation.Name.required"]}).Placeholder(Model["placeholder.Name"]).ShowValidationMessage(true).Label().LabelText(Model["label.Name"])
Nor does this -
@:@form.FormGroup().TextBoxFor(x => x.Name).HtmlAttributes(new { data_val_required = Model["validation.Name.required"]}).Placeholder(Model["placeholder.Name"]).ShowValidationMessage(true).Label().LabelText(Model["label.Name"])
Output for both that do not work is the same:
<input name="Name" class="form-control input-validation-error" id="Name" type="text" placeholder="Enter your name, please" value="" data-val-required="The Name field is required." data-val="true" data-val-length-max="50" data-val-length="The field Name must be a string with a maximum length of 50.">