MVC3有条件禁用Html.TextBoxFor() [英] MVC3 Conditionally disable Html.TextBoxFor()

查看:873
本文介绍了MVC3有条件禁用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆