MVC3有条件禁用Html.TextBoxFor() [英] MVC3 Conditionally disable Html.TextBoxFor()
问题描述
我有一个C#.NET Web应用程序。在该应用程序我需要根据谁登录有条件地禁止 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 或已禁用的。然而,这呈现为 IsDisabled =禁用
或 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屋!