Inconsistent null attribute handling in ASP.NET MVC 4 -
i have been trying handle optional html required
, readonly
attributes in asp.net mvc 4. surprise, found out null
attributes in html helpers rendered empty strings while removed in razor (desired behavior).
for example, code:
@{ string disabled = null; string @readonly = null; } @html.textbox("t1", "value", new { disabled, @readonly }) <input type="text" name="t2" value="value" disabled="@disabled" readonly="@(@readonly)" />
renders:
<input disabled="" id="t1" name="txt1" readonly="" type="text" value="value" /> <input type="text" name="t2" value="value" />
basically want know is:
- what reason behind these 2 different behaviors?
- is there way same result using
html.texbox
without writing custom code?
edit
this not possible without writing custom html helper, there's feature request on codeplex.
the html.textbox()
behavior comes code in system.web.mvc.html
transforms routevaluedictionary
of attributes actual html. (i believe code in tagbuilder
)
the raw html tag behavior comes feature in razor v2 language parser removes attributes in razor markup resolve null
@ runtime.
Comments
Post a Comment