[ACCEPTED]-ASP.NET MVC - Disable Html Helper control using boolean value from Model-asp.net-mvc
This here should do the trick:
<%= Html.TextBox("MyTextbox", Model.MyValuenew,
(Model.IsMyTextboxEnabled() ? (object) new {id = "MyTextbox", @class = "MyClass"}
: (object) new {id = "MyTextbox", @class = "MyClass", disabled="true" })) %>
0
In your helper would you have a check in 11 the code, when you are generating the html, that 10 simply checks the bool and then either adds 9 the disabled attribute or leaves it out?
This 8 is a simply example and not well structrued 7 but...
if (disabled)
return string.Format(CultureInfo.InvariantCulture, "<input type=text disabled/>", new object[] { HttpUtility.HtmlAttributeEncode(s), myTextBox });
Is this what you were asking?
EDIT:
Wait, I 6 see now. I think you'll need to either 5 create your own helper or extend the MVC 4 textbox helper so that you can do it.
Either 3 that or you can I think do something like;
<%= Html.TextBox("mytextbox","", new { disabled="true" } %>
The 2 above is untested but something like that 1 should work.
EDIT 2:
<% if (condition) {%>
<%= Html.TextBox("mytextbox", "", new {@readonly="readonly"}) %>
<%} else {%>
<%= Html.TextBox("mytextbox", "") %>
<%}
Too late maybe but hope it will help:
I´ve 12 been working with ASP MVC recently and (as 11 I refused to have one combination of Anonymous 10 types for each possible set of attributes) realized 9 that, as of MVC 2, every Input Extension 8 method has two forms: one with attributes 7 as Objects and one as IDictionary<string, Object>
element. See the msdn API
So, I 6 ended up coding the following, which personally 5 I find far more convenient that creating 4 multiple objects (actually, for multiple 3 option or checkbox controls you can use 2 just one Dictionary and add or remove properties 1 at will):
<%
IDictionary<string, Object> radioAttrs = new Dictionary<string, Object>();
radioAttrs.Add("class", "radioBot");
if (Model.EnabledRegistro)
{
radioAttrs["onclick"] = "updateControlsForInfoNom(true)";
}
else
{
radioAttrs["disabled"] = "disabled";
}
%>
<%: Html.RadioButtonFor(model => model.Accion.calculoRegistro, "true", radioAttrs)%>
...
With this method you can supply multiple 1 attributes if needed
@Html.TextBox("mytextbox", new {}, new { @class = "myclass", disabled = "true" })
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.