1

我有一个文本区域,我可能想在某些情况下禁用它。我想将此信息作为 ViewBag 参数发送,但我不知道该怎么做。

我认为的 textarea 看起来像这样

@Html.TextAreaFor(f => f.ProgressDetail, new { @class = "followUpProgress", ViewBag.DisableProgressDetail })

在控制器中我有这样的东西:

if(conditions)
    ViewBag.DisableProgressDetail = "disabled=\"disabled\"";

然而,html 输出是这样的:

<textarea DisableProgressDetail="disabled=&quot;disabled&quot;" class="followUpProgress" cols="20" id="ProgressDetail" name="ProgressDetail" rows="2">
</textarea>
4

3 回答 3

3

你想要的是这样的:

@Html.TextAreaFor(f => f.ProgressDetail, new { @class = "followUpProgress", disabled = ViewBag.DisableProgressDetail })

然后在你的控制器中,让它:

ViewBage.DisableProgressDetail = "disabled";
于 2012-06-22T20:32:09.833 回答
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";
于 2012-06-22T20:47:36.537 回答
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" })
于 2014-03-12T20:12:14.400 回答