asp.net mvc - EditorFor htmlAttributes not working for type bool (MVC 5.2) -


since mvc 5.1 possible add html attributes editor templates follows:

@html.editorfor(m => m.foo, new { htmlattributes = new { id = "fooid", @class="fooclass" } }) 

if property foo of type string, generate input markup correctly, including custom attributes.

but if property foo of type bool (or bool?) attributes ignored...

am missing here?. feature still not supported templates generates "select" markup?

i know question asked while ago ran exact same issue now. turns out different developer had created custom editor template boolean in our solution able customize text of drop-down. code supposed work editors out of box not work automatically custom editors ... have implement yourself.

if issue, need modify custom editor template fetch htmlattributes out of viewdata , pass them along underlying dropdownlistfor (or whatever helper using). here custom editor template looks now:

@model bool? @using system.web.mvc; @{     var htmlattributes = viewdata["htmlattributes"] ?? new { };      @html.dropdownlistfor(model => model,         new list<selectlistitem>(3) {          new selectlistitem { text = "unknown", value = "" },         new selectlistitem { text = "yes", value = "true", selected = model.hasvalue && model.value },         new selectlistitem { text = "no", value = "false", selected = model.hasvalue && !model.value }     }, htmlattributes) } 

the credit explanation & solution goes https://cpratt.co/html-editorfor-and-htmlattributes


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 -