在MVC3 Razor视图引擎中设置文本框的可见性

时间:2021-05-23 15:12:32

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,剃刀视图引擎的新手。我想根据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".

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

So please help me in rectifying the above code.

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

4 个解决方案

#1


14  

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

#2


8  

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

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

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

See this example here:

在这里看到这个例子:

http://jsfiddle.net/QxSpU/

http://jsfiddle.net/QxSpU/

#3


2  

(EDITED)

(编者)

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

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

#4


0  

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

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

#1


14  

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

#2


8  

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

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

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

See this example here:

在这里看到这个例子:

http://jsfiddle.net/QxSpU/

http://jsfiddle.net/QxSpU/

#3


2  

(EDITED)

(编者)

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

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

#4


0  

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

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