asp.net-mvc 在 MVC3 Razor 视图引擎中设置文本框的可见性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6505674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:17:35  来源:igfitidea点击:

Setting visibility of a textbox in MVC3 Razor view engine

asp.net-mvcasp.net-mvc-3razorasp.net-mvc-2

提问by Biki

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.

我是 MVC 3,剃刀视图引擎的新手。我想根据视图模型中的值在运行时设置文本框的可见性。

But the below code is not working.

但是下面的代码不起作用。

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

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

一旦上面的代码开始工作,我就可以代替@Model.EnableCompanyName硬编码的“false”。

So please help me in rectifying the above code.

所以请帮我纠正上面的代码。

回答by Chris

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

这将根据您的 bool Model.EnableCompanyName 更改显示类型:)

Hope it helps!

希望能帮助到你!

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

回答by DavidGouge

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

这与剃须刀本身无关。visible不是input元素的有效属性(这是 Html.TextBox 将生成的)。你需要

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

See this example here:

在此处查看此示例:

http://jsfiddle.net/QxSpU/

http://jsfiddle.net/QxSpU/

回答by Yuriy Naydenov

Updated:

更新:

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

回答by Ashes

Try this:

尝试这个:

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