[英]ASP.NET MVC 5 How to remove or add attribute in Html.TextBoxFor
In my MVC Application View I have @Html.TextBoxFor(m => m.Email, new { @class = "form-control", @readonly = "readonly" })
在我的MVC应用程序视图中,我有
@Html.TextBoxFor(m => m.Email, new { @class = "form-control", @readonly = "readonly" })
I need to remove the readonly-attribute when an if-statement occurs in my Controller. 当控制器中出现if语句时,我需要删除readonly属性。 How can I, from the Controller, remove the attribute in the View, or add the attribute if I first remove it from the View and then change my if-statement?
我如何从控制器中删除视图中的属性,或者如果我先从视图中删除属性,然后更改我的if语句,则添加该属性?
I understand that you want to conditionally add the readonly
attribute to a textbox. 我了解您想有条件地将
readonly
属性添加到文本框。
You may use ViewData
for that, from your controller assign ViewData["IsEmailReadOnly"]
to a boolean value 您可以为此使用
ViewData
,从您的控制器ViewData["IsEmailReadOnly"]
分配给布尔值
if(MyCondition())
ViewData["IsEmailReadOnly"] = true;
else
ViewData["IsEmailReadOnly"] = false;
And then inside your View: 然后在您的视图中:
@{
object textBoxAttrs;
if((bool) ViewData["IsEmailReadOnly"]) {
textBoxAttrs = new { @class = "form-control", @readonly = "readonly" };
}
else
{
textBoxAttrs = new { @class = "form-control" };
}
}
@Html.TextBoxFor(m => m.Email, textBoxAttrs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.