我有一个textarea,我可能想在某些条件下禁用。我想将此信息作为ViewBag参数发送,但我无法弄清楚如何做到这一点。
我视图中的textarea看起来像这样
@Html.TextAreaFor(f => f.ProgressDetail, new { @class = "followUpProgress", ViewBag.DisableProgressDetail })
在控制器中我有这样的东西:
if(conditions)
ViewBag.DisableProgressDetail = "disabled=\"disabled\"";
然而,html输出是这样的:
<textarea DisableProgressDetail="disabled="disabled"" class="followUpProgress" cols="20" id="ProgressDetail" name="ProgressDetail" rows="2">
</textarea>
答案 0 :(得分:3)
你想要的是这个:
@Html.TextAreaFor(f => f.ProgressDetail, new { @class = "followUpProgress", disabled = ViewBag.DisableProgressDetail })
然后在您的控制器中,只需创建它:
ViewBage.DisableProgressDetail = "disabled";
答案 1 :(得分:1)
如果未指定属性,则来自属性的名称,这就是您获取为ViewBag属性命名的html属性的原因。让它运作的一种方法是:
// in the view:
@Html.TextAreaFor(f => f.ProgressDetail, new { @class = "followUpProgress", ViewBag.disabled })
-------------------------------------------------------------
// in the controller
ViewBag.disabled = "disabled";
如果你不喜欢这种方法,你可以像这样设置禁用位:
// in the view:
@Html.TextAreaFor(f => f.ProgressDetail, new { @class = "followUpProgress", disabled=ViewBag.DisableProgressDetail })
-------------------------------------------------------------
// in the controller:
if(conditions)
ViewBag.DisableProgressDetail = "disabled";
else
ViewBag.DisableProgressDetail = "false";
// or more simply
ViewBag.DisableProgressDetail = (conditions) ? "disabled" : "false";
答案 2 :(得分:1)
它不起作用。你可以试试这个:
//In the controller
if(Mycondition){ ViewBag.disabled = true;}
else { ViewBag.disabled = false;}
//In the view
@Html.TextBoxFor(model => model.MyProperty, ViewBag.disabled ? (object)new { @class = "MyClass", size = "20", maxlength = "20", disabled = "disabled" } : (object)new { @class = "MyClass", size = "20", maxlength = "20" })