Define this somewhere in your view/helpers
@functions {
object getHtmlAttributes (bool ReadOnly, string CssClass)
{
if (ReadOnly) {
return new { @class = CssClass, @readonly = "readonly" };
}
return new { @class = CssClass };
}
}
Then use :
@Html.TextBox("name", "value", @getHtmlAttributes(Model.ReadOnly, "test"))