view - Custom Editor template based on ViewModel Dataannotation attribute MVC4 -


what want automatically add image span after input textboxes if [required] attribute decorates viewmodel property integer, double, string, date etc

for example, viewmodel might like

public class myviewmodel  {     [required]     public string  name { get; set; } } 

and view like

@html.editorfor(model => model.name) @html.validationmessagefor(model => model.name) 

and output like

<input id="name" class="text-box single-line" type="text" value="" name="name" data-val-required="the name field required." data-val-length-max="20" data-val-length="the field name must string maximum length of 20." data-val="true"> <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="name"></span>  -- note automatically added span <span class="indicator required" style="width: 11px;"></span> 

i intending have css show image i.e.

span.required {     background-image: url("required.png"); } 

is possible or need create own helper method implement type of functionality?

yes, it's possible, in general wouldn't recommend it, because templates there customize type rendering, , should able create templates without worrying if overrides template.

i instead create custom labelfor helper, such 1 described here:

http://weblogs.asp.net/imranbaloch/archive/2010/07/03/asp-net-mvc-labelfor-helper-with-htmlattributes.aspx

or here:

http://weblogs.asp.net/raduenuca/archive/2011/02/17/asp-net-mvc-display-visual-hints-for-the-required-fields-in-your-model.aspx

a third option not in mvc, rather add javascript add indicator based on standard mvc validation data attributes (if you're using unobtrusive validation). see answer here:

https://stackoverflow.com/a/8524547/61164


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -