i'm using c#/.net in visual studio , mvc framework.
i have form i'm working with, , able output correct error message @ appropriate time. issue input field of form supposed highlight red has-error class when field either not filled in or filled in incorrectly. if using regular form tags, because connected umbraco project, have use html.editorfor , html.validationmessagefor.
is there way (either using jquery or modifying existing code) add class html.editorfor field upon html.validationmessagefor deciding should display message?
here portion of view:
<div class="name-field"> <label>name</label> @html.editorfor(m => m.name, new { htmlattributes = new { @class = "form-control", @id = "contact_name", @name = "contact_name", @placeholder = "name" } }) @if (@html.validationmessagefor(model => model.name) != null) { @html.validationmessagefor(model => model.name, null, new { @class = "contact-form-error-msg text-danger small-sized", @id = "contact-form-name-error-1", @style = "display:block;" }) } </div>
and here attribute in model:
[required(errormessage = "please enter contact name.")] public string name { get; set; }
like said, error message shown in model is showing correctly. issue applying has-error class html.editorfor field have feeling going require jquery; however, i'm not sure how access existing code in jquery block.
thank you.
edit: notified question possible duplicate of question here (how can check if model valid inside razor view?). modification able find solution problem.
@if (!viewdata.modelstate.isvalidfield("name")) { <div class="name-field has-error"> @html.editorfor(m => m.name, new { htmlattributes = new { @class = "form-control", @id = "contact_name", @name = "contact_name", @placeholder = "name" } }) @if (@html.validationmessagefor(model => model.name) != null) { @html.validationmessagefor(model => model.name, null, new { @class = "contact-form-error-msg text-danger small-sized", @id = "contact-form-name-error-1", @style = "display:block;" }) } </div> } else { <div class="name-field"> @html.editorfor(m => m.name, new { htmlattributes = new { @class = "form-control", @id = "contact_name", @name = "contact_name", @placeholder = "name" } }) </div> }
Comments
Post a Comment