I have this line of code:
@Html.EditorFor(model => model.Quantity, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
I have a variable in my view data dictionary called Readonly. How do I make Quantity read-only if ViewBag.Readonly
is true and not read only if it is false?
Simple thing, but the combination of Razor with HTML (which is ancient) makes otherwise simple things impossible.
Edits:
I don't want to use an if statement. That is a last resort because it violates DRY which I have been severely burned many times in the past for not following.
The line I have above does work insofar as it makes the text box read-only. I need to make this conditional based upon my view state.
Solution:
I've used the following. It still is a DRY violation, but it reduces it to one line.
@Html.EditorFor(model => model.Quantity, new { htmlAttributes = ViewBag.Readonly ? (object)new { @class = "form-control", @readonly = "htmlsucks" } : (object)new { @class = "form-control" } })