views:

1382

answers:

3

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?

+2  A: 

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
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
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
+4  A: 

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
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
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
maybe its the @class....should be able to just specify class
Simon Fox
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
updated, cheers!
Simon Fox
A: 

See My Answer here: Disabled Textbox

Lavinski