Asp.net mvc 如何使用ViewBag或Model值在mvc中添加HtmlAttributes?
我有一个文本区域,在某些情况下可能要禁用它。我想将此信息作为ViewBag参数发送,但我不知道如何发送 我视图中的文本区域如下所示Asp.net mvc 如何使用ViewBag或Model值在mvc中添加HtmlAttributes?,asp.net-mvc,razor,viewbag,Asp.net Mvc,Razor,Viewbag,我有一个文本区域,在某些情况下可能要禁用它。我想将此信息作为ViewBag参数发送,但我不知道如何发送 我视图中的文本区域如下所示 @Html.TextAreaFor(f => f.ProgressDetail, new { @class = "followUpProgress", ViewBag.DisableProgressDetail }) 在控制器中,我有如下内容: if(conditions) ViewBag.DisableProgressDetail = "disab
@Html.TextAreaFor(f => f.ProgressDetail, new { @class = "followUpProgress", ViewBag.DisableProgressDetail })
在控制器中,我有如下内容:
if(conditions)
ViewBag.DisableProgressDetail = "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";
但是,html输出如下所示:
<textarea DisableProgressDetail="disabled="disabled"" class="followUpProgress" cols="20" id="ProgressDetail" name="ProgressDetail" rows="2">
</textarea>
你想要的是:
@Html.TextAreaFor(f => f.ProgressDetail, new { @class = "followUpProgress", disabled = ViewBag.DisableProgressDetail })
然后在控制器中,只需将其设置为:
ViewBage.DisableProgressDetail = "disabled";
你想要的是:
@Html.TextAreaFor(f => f.ProgressDetail, new { @class = "followUpProgress", disabled = ViewBag.DisableProgressDetail })
然后在控制器中,只需将其设置为:
ViewBage.DisableProgressDetail = "disabled";
如果未指定属性,则该属性来自属性的名称,这就是为什么您会获得一个为ViewBag属性命名的html属性。让它工作的一种方法是:
// in the view:
@Html.TextAreaFor(f => f.ProgressDetail, new { @class = "followUpProgress", ViewBag.disabled })
-------------------------------------------------------------
// in the controller
ViewBag.disabled = "disabled";
如果您不喜欢这种方法,可以将禁用位设置为:
if(conditions)
ViewBag.DisableProgressDetail = "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";
如果未指定属性,则该属性来自属性的名称,这就是为什么您会获得一个为ViewBag属性命名的html属性。让它工作的一种方法是:
// in the view:
@Html.TextAreaFor(f => f.ProgressDetail, new { @class = "followUpProgress", ViewBag.disabled })
-------------------------------------------------------------
// in the controller
ViewBag.disabled = "disabled";
如果您不喜欢这种方法,可以将禁用位设置为:
if(conditions)
ViewBag.DisableProgressDetail = "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";
它不起作用。您可以尝试以下方法:
//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" })
它不起作用。您可以尝试以下方法:
//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" })
当我看到一个新的答案出现时,我实际上正准备把它作为我自己的答案发布当我看到一个新的答案出现时,我实际上正准备把它作为我自己的答案发布