13

I am outputting a textbox to the page using the Html helpers. I want to add the disabled attribute dynamically based on whether or not a boolean value in my model is true or false.

My model has a method that returns a boolean value:

<% =Model.IsMyTextboxEnabled() %>

I currently render the textbox like follows, but I want to now enabled or disable it:

<% =Html.TextBox("MyTextbox", Model.MyValuenew { id = "MyTextbox", @class = "MyClass" })%>

If the return value of Model.IsMyTextboxEnabled() == true I want the following to be output:

<input class="MyClass" id="MyTextbox" name="MyTextbox" type="text" value="" />

If it == false, I want it to output as:

<input class="MyClass" id="MyTextbox" name="MyTextbox" type="text" value="" disabled />

What is the cleanest way to do this?

The Matt
  • 6,618
  • 7
  • 44
  • 61

4 Answers4

16

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" })) %>
Simon Fox
  • 10,409
  • 7
  • 60
  • 81
  • I think you're on the right track, but that throws a syntax error that says "Type of conditional expression cannot be determined because there is no implicit conversion between AnonymousType#1 and AnonymousType#2" – The Matt Aug 20 '09 at 00:01
  • Probably to do with the second parameter. I wasn't sure from your question what it was because there seems to be a comma missing? Is Model.MyValuenew the value you want in the field? If so it should be the second parameter otherwise you should be able to just pass null. I have tested the third parameter and it works giving you the behaviour you asked for – Simon Fox Aug 20 '09 at 00:15
  • Figured it out. You just need to put a cast to (object) before each 'new'. See http://stackoverflow.com/questions/177673/html-textbox-conditional-attribute-with-asp-net-mvc-preview-5. If you want to update your answer, I'll accept it for you. – The Matt Aug 20 '09 at 00:18
  • But this will not enable textbox eventhough disabled="false". It will get enabled only when enabled="true". So you will have to write to lines for render control, each for enabled = "true" and disabled = "true". – Altaf Patel Jun 01 '16 at 08:56
3

In your helper would you have a check in the code, when you are generating the html, that simply checks the bool and then either adds the disabled attribute or leaves it out?

This is a simply example and not well structrued 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 see now. I think you'll need to either create your own helper or extend the MVC textbox helper so that you can do it.

Either that or you can I think do something like;

<%= Html.TextBox("mytextbox","", new { disabled="true" } %>

The above is untested but something like that should work.

EDIT 2:

<% if (condition) {%>
  <%= Html.TextBox("mytextbox", "", new {@readonly="readonly"}) %>
<%} else {%>
  <%= Html.TextBox("mytextbox", "") %>
<%}
griegs
  • 22,624
  • 33
  • 128
  • 205
  • I can't extend the MVC helper because the attribute object that comes in is an anonymous type and I can't dynamically append a 'disabled' attribute to it. As for the second suggestion, I've tried that but it doesn't work either because the disabled attribute either exists in the tag or it doesn't -- there's no disabled='true' or disabled='false'. – The Matt Aug 19 '09 at 23:52
  • I've done a little bit of testing and yeah you can't do that so I think you have two options. 1) Create your own Helper which takes the disabled state as an argument and then renders your textbox. or 2) have code which, depending on state, renders your textbox with the disabled property or not. Check the 2nd edit above. – griegs Aug 20 '09 at 01:11
3

Too late maybe but hope it will help:

I´ve been working with ASP MVC recently and (as I refused to have one combination of Anonymous types for each possible set of attributes) realized that, as of MVC 2, every Input Extension method has two forms: one with attributes as Objects and one as IDictionary<string, Object> element. See the msdn API

So, I ended up coding the following, which personally I find far more convenient that creating multiple objects (actually, for multiple option or checkbox controls you can use just one Dictionary and add or remove properties 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)%>
  ...
Frederic
  • 88
  • 6
0

With this method you can supply multiple attributes if needed

@Html.TextBox("mytextbox", new {}, new { @class = "myclass", disabled = "true" })
nuander
  • 1,319
  • 1
  • 19
  • 33