MVC3有条件地禁用Html.TextBoxFor()

Mik*_*ebb 28 conditional html.textbox asp.net-mvc-3

我有一个C#.Net网络应用程序.在该应用程序中,我需要根据登录系统的人有条件地禁用Html.TextBoxFor控件(也包括Html.DropDownListFor控件).我试过用

 @Html.TextBoxFor(model => model.ProposalName, new { @ViewBag.IsDisabled })
Run Code Online (Sandbox Code Playgroud)

在Controller中@ViewBag.IsDisabled设置为String.Empty"disabled"的位置.然而,这呈现为IsDisabled='disabled'IsDisabled=""因此控制没有被禁用.当我尝试

@Html.TextBoxFor(model => model.ProposalName, new { @ViewBag.Disabled })
Run Code Online (Sandbox Code Playgroud)

即使不ViewBag.Disabled包含文本,控件也始终处于禁用状态.如何有条件地禁用Html.TextBoxFor()控件?

epi*_*isx 56

尝试

@Html.TextBoxFor(model => model.ProposalName, ViewBag.Disabled ? (object)new { disabled="disabled" } : new {})
Run Code Online (Sandbox Code Playgroud)


Die*_*ego 20

@epignosisx发布的解决方案有效,但如果你想添加一些其他属性可能会有问题,因为你必须将它添加到两个对象(一个对象disabled和一个现在为空).

更糟糕的是,如果你有一些其他bool属性,因为你将有四个不同的对象,每个对应一个.

这里的最佳解决方案(使用更多代码)是为HtmlHelper构建一个扩展方法,以接收您的布尔属性作为参数.

public static MvcHtmlString TextBoxDisabledFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, bool disabled, object htmlAttributes = null)
{
    return TextBoxDisabledFor(htmlHelper, expression, disabled, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}

public static MvcHtmlString TextBoxDisabledFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, bool disabled, IDictionary<string, object> htmlAttributes)
{
    if (htmlAttributes == null)
        htmlAttributes = new Dictionary<string, object>();
    if (disabled)
        htmlAttributes["disabled"] = "disabled";
    return htmlHelper.TextBoxFor(expression, htmlAttributes);
}
Run Code Online (Sandbox Code Playgroud)

这里有另一个例子


Jam*_*mes 10

我有同样的问题,并决定编写自己的HtmlHelper扩展方法.

public static MvcHtmlString Disable(this MvcHtmlString helper, bool disabled)
    {
        if (helper == null)
            throw new ArgumentNullException();

        if (disabled)
        {
            string html = helper.ToString();
            int startIndex = html.IndexOf('>');

            html = html.Insert(startIndex, " disabled=\"disabled\"");
            return MvcHtmlString.Create(html);
        }

        return helper;
    }
Run Code Online (Sandbox Code Playgroud)

这将接受一个布尔值来指示是否应该禁用该控件.它只是附加disabled="disabled"在第一个内部>在字符串中出现一个内容中.

您可以像下面一样使用它.

@Html.TextBoxFor(model => model.ProposalName).Disable(true)


小智 9

这是我使用的方法,它不需要扩展,也不仅限于一个HTML属性.它假设你的模型中有一个名为"Disabled"的布尔属性,但你可以在那里放置任何你想要的东西,只要它为三元运算符求值为boolean:

@Html.TextBoxFor(model => model.Whatever,
  new Dictionary<string, object>() {
    { "size", "5" },
    { "class", "someclasshere" },
    { model.Disabled ? "disabled" : "data-notdisabled", "disabled" }
  })
Run Code Online (Sandbox Code Playgroud)

标准快捷方式表示法的限制是属性的名称不能是动态的.通过创建正确类型的字典,您可以将属性名称设置为动态,然后将该字典作为属性字典传递到文本框.当不禁用该字段时,它会传递名为"data-notdisabled"的属性,而不是名为"disabled"的属性,然后浏览器将忽略该属性.


Ext*_*rey 6

扩展@ James的答案,我写了这个HtmlHelper扩展,disabled如果它已经存在则更新/删除属性,或者如果没有则添加它:

public static MvcHtmlString Disable(this MvcHtmlString helper, bool disabled) {
    string html = helper.ToString();
    var regex = new Regex("(disabled(?:=\".*\")?)");
    if (regex.IsMatch(html)) {
        html = regex.Replace(html, disabled ? "disabled=\"disabled\"" : "", 1);
    } else {
        regex = new Regex(@"(\/?>)");
        html = regex.Replace(html, disabled ? "disabled=\"disabled\"$1" : "$1", 1);
    }
    return MvcHtmlString.Create(html);
}
Run Code Online (Sandbox Code Playgroud)

它也可以很好地与自闭标签(如<input />)一起使用.

用法是一样的:

@Html.TextBoxFor(model => model.PropertyName).Disable(true)
Run Code Online (Sandbox Code Playgroud)

测试了@Html.DropDownListFor()@Html.TextBoxFor().