Setting text box visibility in the MVC3 Razor view engine

I am new to MVC 3, a razor viewing engine. I want to set the visibility of a text field at runtime according to the value in my view model.

But the code below does not work.

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

Once the above code starts working, I can place @Model.EnableCompanyName instead of hardcoded "false".

So please help me in fixing the code above.

+7
source share
4 answers

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

Hope this helps!

 @{ String displayMode = (Model.EnableCompanyName) ? "inline" : "none"; @Html.TextBox("CompanyName", "", new { style = "display:" + displayMode + ";" }) } 
+14
source

This is not related to the razor per se. visible not a valid attribute for an input element (which will generate an Html.TextBox). You need

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

Look here:

http://jsfiddle.net/QxSpU/

+9
source

(Edited)

@Html.TextBox("CompanyName", "", new { style = Model.EnableCompanyName ? "display:inline" : "display:none" })

+2
source

Add @ Html.TextBox ("CompanyName", ", new {Style = Model.EnableCompanyName?" Visibility: visible ":" visibility: hidden "})

0
source

All Articles