Setting visibility of a textbox in MVC3 Razor view engine

守給你的承諾、 提交于 2019-12-09 12:19:11

问题


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.


回答1:


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 + ";" })
}



回答2:


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/




回答3:


(EDITED)

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




回答4:


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



来源:https://stackoverflow.com/questions/6505674/setting-visibility-of-a-textbox-in-mvc3-razor-view-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!