Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling & disabling a textbox in razor view (ASP.Net MVC 3)

I want to Enable or Disable a textbox based on the value (Model.CompanyNameEnabled).

The below code is not working. Please rectify.

@{
    string displayMode = (Model.CompanyNameEnabled) ? "" : "disabled = disabled";
    @Html.TextBox("CompanyName", "", new { displayMode })
}
like image 287
Biki Avatar asked Jul 06 '11 12:07

Biki


3 Answers

@{
   object displayMode = (Model.CompanyNameEnabled) ? null : new {disabled = "disabled" };
   @Html.TextBox("CompanyName", "", displayMode)
}

You should pass htmlAttribute as anonymous object, with property names = html attribute names, property values = attribute values. Your mistake was that you were passing string instead of name=value pair

like image 180
archil Avatar answered Sep 21 '22 16:09

archil


<input id="textbox1" type="text" @{@((Model.CompanyNameEnabled) ? null : new { disabled = "disabled" })}; />

Haven't tested it, but should work

like image 27
user1985065 Avatar answered Sep 20 '22 16:09

user1985065


A simple approach:

@Html.TextBoxFor(x => x.Phone, new { disabled = "disabled", @class = "form-control" })

like image 33
ransems Avatar answered Sep 22 '22 16:09

ransems