c# - input-validation-error class not added to custom HtmlHelper Extension Method -


i wrote own htmlhelper extension method display time-input.

public static mvchtmlstring timepickerfor<tmodel, tproperty>(this htmlhelper<tmodel> htmlhelper, expression<func<tmodel, tproperty>> expression)         {             if (expression == null)             {                 throw new argumentnullexception("expression");             }              string expressiontext = expressionhelper.getexpressiontext(expression);             string value = modelmetadata.fromlambdaexpression(expression, htmlhelper.viewdata).model.tostring();             return timepicker(htmlhelper, expressiontext, value);         }          public static mvchtmlstring timepicker(this htmlhelper helper, string name, string value)         {             tagbuilder builder = new tagbuilder("input");             builder.mergeattribute("type", "time");             builder.mergeattribute("id", name);             builder.mergeattribute("name", name);              if (value != null)             {                 builder.mergeattribute("value", value);             }              return mvchtmlstring.create(builder.tostring(tagrendermode.selfclosing));         } 

this how use it:

@html.timepickerfor(model => model.starttime) 

when time not valid, add error this:

modelstate.addmodelerror("starttime", "the selected time taken."); 

when error added, should add input-validation-error class input, doesn't. cannot work. show error in validationsummary, , adds class default inputs. appreciated!

i tried adding this, still won't add class:

var validation = helper.getunobtrusivevalidationattributes(name); foreach (keyvaluepair<string, object> attribute in validation) {     builder.mergeattribute(attribute.key, attribute.value.tostring()); } 

edit: temporary workaround found, haven't been able solve problem though

modelstate modelstate; if (helper.viewdata.modelstate.trygetvalue(name, out modelstate)) {     if (modelstate.errors.count > 0)          builder.addcssclass("input-validation-error"); } 

what doing not workaround! how supposed it

string fullname = helper.viewcontext.viewdata.templateinfo.getfullhtmlfieldname(name); modelstate modelstate; if (helper.viewdata.modelstate.trygetvalue(fullname, out modelstate)) {     if (modelstate.errors.count > 0)     {         builder.addcssclass(htmlhelper.validationinputcssclassname);     } } 

the purpose of getunobtrusivevalidationattributes return attributes unobtrusive validation work. these attributes start name "data-val" , used jquery.validate.unobtrusive.js

<input data-val="true" data-val-required="please supply title." id="title" name="title" type="time"> 

your custom helper must have both in order work properly.

for reference, read source code of inputextensions here

but wait!

html5 time input natively supported mvc! don't need write own.

public class testmodel {     [datatype(datatype.time)]     public datetime mytime { get; set; } }  @html.editorfor(model => model.mytime) 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -