MVC3 有条件地禁用 Html.TextBoxFor() [英] MVC3 Conditionally disable Html.TextBoxFor()
问题描述
我有一个 C# .Net 网络应用程序.在该应用程序中,我需要根据登录到系统的人员有条件地禁用 Html.TextBoxFor
控件(也是 Html.DropDownListFor
控件).我尝试使用
I have a C# .Net web app. In that app I need to conditionally disable Html.TextBoxFor
controls (also Html.DropDownListFor
controls) based on who is logged into the system. I tried using
@Html.TextBoxFor(model => model.ProposalName, new { @ViewBag.IsDisabled })
其中 @ViewBag.IsDisabled
在控制器中设置为 String.Empty 或 "disabled".但是,这呈现为 IsDisabled='disabled'
或 IsDisabled=""
因此控件未被禁用.当我尝试时
Where @ViewBag.IsDisabled
is set to either String.Empty or "disabled" in the Controller. However, this renders as IsDisabled='disabled'
or IsDisabled=""
so the control is not disabled. When I tried
@Html.TextBoxFor(model => model.ProposalName, new { @ViewBag.Disabled })
即使 ViewBag.Disabled
不包含任何文本,该控件也始终处于禁用状态.如何有条件地禁用 Html.TextBoxFor()
控件?
The control was always disabled even if ViewBag.Disabled
contained no text. How can I conditionally disable the Html.TextBoxFor()
controls?
推荐答案
试试
@Html.TextBoxFor(model => model.ProposalName, ViewBag.Disabled ? (object)new { disabled="disabled" } : new {})
这篇关于MVC3 有条件地禁用 Html.TextBoxFor()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!