I want to disable all fields in my form, which have values when page is loaded.
For example in this
<td>@Html.TextBoxFor(m => m.PracticeName, new { style = "width:100%", disabled = Model.PracticeName == String.Empty ? "Something Here" : "disabled" })</td>
I want to write inline something like this. I don’t want to use if-else and make my code larger.
Using javascript/jquery doesn’t welcome too.
I tried to write false/true, but 1.It maybe isn’t cross-browser 2.Mvc parsed it to string like “True” and “False”.
So how can I do it?
P.S. I use ASP.NET MVC 3 🙂
Seems like a good candidate for a custom helper:
which could be used like this:
The helper could obviously be taken a step further so that you don’t need to pass an additional boolean value but it automatically determines whether the value of the expression is equal to
default(TProperty)and it applies thedisabledattribute.Another possibility is an extension method like this:
which you would use with the standard
TextBoxForhelper: