动态设置TextBoxFor HtmlHelper的禁用html属性

时间:2012-04-18 10:28:22

标签: asp.net-mvc-3

我正在尝试为disabled HtmlHelper

动态设置TextBoxFor属性
@Html.TextBoxFor(model => model.Street, 
                 new
                 {
                    @class = "", 
                    disabled = (Model.StageID==(int)MyEnum.Sth) ? "disabled" : "" 
                 })

但即使有disabled="",也与disabled="disabled"相同。怎么解决这个问题?

4 个答案:

答案 0 :(得分:20)

我在一个月前遇到同样的问题,我最后使用了这个扩展方法

public static class AttributesExtensions
{
    public static RouteValueDictionary DisabledIf(
        this object htmlAttributes, 
        bool disabled
    )
    {
        var attributes = new RouteValueDictionary(htmlAttributes);
        if (disabled)
        {
            attributes["disabled"] = "disabled";
        }
        return attributes;
    }
}

之后你可以像这样使用它

@Html.TextBoxFor(
    model => model.Street, 
    new { @class = "" }.DisabledIf(Model.StageID==(int)MyEnum.Sth)
)

编辑Paul之后comment):

使用data-xxx html属性可以使用System.Web.Routing.RouteValueDictionary类的构造函数进行挖掘,因为下划线不会自动转换为减号。

使用方法System.Web.Mvc.HtmlHelper.AnonymousObjectToHtmlAttributes:将解决此问题。

更新代码(仅限扩展方法正文)

var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
if (disabled)
{
    attributes["disabled"] = "disabled";
}
return attributes;

答案 1 :(得分:0)

使用下面的扩展方法会产生类似的结果,但这可能更脆弱:

@Html.TextBoxFor(
     model => model.Street, 
     new { @class = "form-control" }
).DisabledIf(Model.IsReadOnly)

扩展:

using System.Text.RegularExpressions;
using System.Web.Mvc;

namespace xxx.HtmlHelpers
{
    public static class MvcHtmlStringExtensions
    {

        private static readonly Regex OpeningTagPattern;

        static MvcHtmlStringExtensions()
        {
            OpeningTagPattern = new Regex("<[a-zA-Z]*");
        }

        public static MvcHtmlString DisabledIf(this MvcHtmlString controlHtml, bool isDisabled)
        {
            if (!isDisabled) return controlHtml;
            return
                new MvcHtmlString(OpeningTagPattern.Replace(controlHtml.ToString(),
                    x => string.Format("{0} disabled=\"disabled\"", x.Groups[0])));
        }

    }
}

答案 2 :(得分:0)

可能你的舞台ID没有设定

@{ 
    if(Model.StageID != null &&   Model.StageID > 0)
    {
        @Html.TextBoxFor(model => model.Street, 
             new
             {
                @class = "", 
                disabled = (Model.StageID==(int)MyEnum.Sth) ? "disabled" : "" 
             })
    }else{

        @Html.TextBoxFor(model => model.Street, 
             new
             {
                @class = ""
             })
    }
}

答案 3 :(得分:0)

我们实际上遇到了同样的问题。我们最终实现了一个带有重载参数的扩展方法,它接受一个布尔值,指示我们是否要禁用该控件。我们只是添加&#34;禁用&#34;在适当的时候使用属性,让内置的HtmlHelper处理繁重的工作。

扩展类和方法:

CONFLICTS_myfile

然后,您只需要引用新课程并致电using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Web.Mvc; using System.Web.Mvc.Html; using System.Web.Routing; public static class OurHtmlHelpers { public const string DisabledAttribute = "disabled"; public static MvcHtmlString TextBoxFor<TModel, TProp>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProp>> expression, object htmlAttributes, bool canEdit) { var htmlAttributeDictionary = SetDisabledAttribute(htmlAttributes, canEdit); return htmlHelper.TextBoxFor(expression, htmlAttributeDictionary); } private static RouteValueDictionary SetDisabledAttribute(object htmlAttributes, bool canEdit) { var htmlAttributeDictionary = new RouteValueDictionary(htmlAttributes); if (!canEdit) { htmlAttributeDictionary.Add(DisabledAttribute, DisabledAttribute); } return htmlAttributeDictionary; } }

值得注意的是,您必须为您要使用的任何TextBoxFor重载定义这些扩展,但这似乎是一个合理的权衡。您还可以将大部分相同的代码用于您想要添加功能的其他HtmlHelper。