Hi I have a Html.TextBox() and I need to make it disabled in certain conditions. With the TextArea it goes like this:
<%=primaryLang ? Html.TextArea("e.location", new { rows = 2, cols = 60 }) : Html.TextArea("e.location", new { rows = 2, cols = 60, disabled = "true" })%>
But with TextBox it is not possible:
<%=primaryLang ?
Html.TextBox("e.startDate") :
Html.TextBox("e.startDate", new { disabled = "true"})%>
It will issue {disabled=true} in the value. This is because the only function which will allow you to pass the HtmlAttributes will require also the model to be passed. This view is strongly typed, and the model is automatically filled in.
If I pass it like this:
Html.TextBox("e.startDate", Model.e.startDate, new { disabled = "true"})
or like this:
Html.TextBox("e.startDate", null, new { disabled = "true"})
the GET version will work, but the POST version will issue a NullReferenceException. Both the above seem to have the exact same effect. Both will present the correct data from the model on GET.
If I leave it lust like this:
Html.TextBox("e.startDate")
it will work correctly, for both POST and GET...
Why? Any ways to accomplish?
Thanks! :)
Thanks to the answers below, I solved it like this:
<%=primaryLang ?
Html.TextBox("e.startDate") :
Html.Hidden("e.startDate") + Html.TextBox("e.startDate", null, new { disabled = "true"})%>