How to set disabled in MVC htmlAttribute

Posted by Ollie on Stack Overflow See other posts from Stack Overflow or by Ollie
Published on 2010-03-23T12:34:33Z Indexed on 2010/03/23 12:53 UTC
Read the original article Hit count: 1190

Filed under:
|

When using an HTML Helper, what is the best method to set an attribute based on a condition. For example

<%if (Page.User.IsInRole("administrator")) {%>
<%=Html.TextBoxFor(m => m.FirstName, new {@class='contactDetails'}%>
<%} else {%>
<%=Html.TextBoxFor(m => m.FirstName, new {@class='contactDetails', disabled = true}%>
<%}%>

There must be a better way to programmatically add just one additional KeyPair to the anonymous type? Can't use

new { .... disabled = Page.User.IsInRole("administrator") ... }

as the browser takes any disabled attribute value as making the input disabled

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about c#