Conditionally disable a Control in ASP.NET MVC

In MVC, you can disable a control based on some property from the model using the following

<%: Html.TextBoxFor(m => m.date2,Model.VoiceMessage? null: new { @disabled = true })%>


Here, I am disabling the textbox based on the "VoiceMessage" field. It adds the "disabled" attribute, if "VoiceMessage" value is false.
If any other attributes are present then you can write like  below

<%: Html.TextBoxFor(m => m.date2, Model.VoiceMessage?(object) new { @id = "textbox1" } : (object)new { @id = "textbox1",@disabled=true })%>

Gopikrishna

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment