ASP.NET MVC - Disable Html Helper control using boolean value from Model

Posted by The Matt on Stack Overflow See other posts from Stack Overflow or by The Matt
Published on 2009-08-19T23:39:52Z Indexed on 2010/04/30 2:27 UTC
Read the original article Hit count: 297

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about html