Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting visibility of a textbox in MVC3 Razor view engine

I am new to MVC 3, razor view engine. I want to set the visibility of a textbox at runtime as per the value in my viewmodel.

But the below code is not working.

<td>
    @Html.TextBox("CompanyName", "", new { visible = "false" })
</td>

Once above code starts working, I could place @Model.EnableCompanyName in place of hardcoded "false".

So please help me in rectifying the above code.

like image 616
Biki Avatar asked Jun 28 '11 11:06

Biki


2 Answers

This will change the display type based on your bool Model.EnableCompanyName :)

Hope it helps!

@{
String displayMode = (Model.EnableCompanyName) ? "inline" : "none";
@Html.TextBox("CompanyName", "", new { style = "display:" + displayMode + ";" })
}
like image 60
Chris Avatar answered Sep 22 '22 14:09

Chris


It's nothing to do with razor as such. visible is not a valid attribute for an input element (which is what Html.TextBox will be generating). You need

@Html.TextBox("CompanyName", "", new { style = "display:none;" })

See this example here:

http://jsfiddle.net/QxSpU/

like image 26
DavidGouge Avatar answered Sep 22 '22 14:09

DavidGouge