I want to set the attribute based on some bool value in my view page like :
@{
object displayMode = (Model.PostCodeEnabled) ? null : new { disabled = "disabled", style = "width:200px;" };
@Html.TextBox("PostalCode", "", displayMode)
}
The above code is working fine, but if I want to apply width as 200px in both the case, then the following code is not working:
@{
object displayMode = (Model.PostCodeEnabled) ? new { style = "width:200px;" }: new { disabled = "disabled", style = "width:200px;" };
@Html.TextBox("PostalCode", "", displayMode)
}
Also I m little confused when to append @ symbol before the attribute that is the difference between new { disabled = “disabled”} & new { @disabled = “disabled”}.
The problem is not the @ signs – your problem is that you can’t do implicit type conversion between two anonymous types.
This means, that you need to make a “proper” if statement, like this:
Going back to the @ signs – you always need to include it when creating an anonymous type to use as HTML attributes.