8

I'm trying to dynamically set the disabled attribute for the TextBoxFor HtmlHelper

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

but even if there is disabled="" it is the same as disabled="disabled". How to get around of this ?

4

4 回答 4

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)
)

编辑(在保罗评论之后):

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

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

更新的代码(仅限扩展方法主体)

var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
if (disabled)
{
    attributes["disabled"] = "disabled";
}
return attributes;
于 2012-04-18T10:34:04.580 回答
0

Probably your stage Id is not getting set

@{ 
    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 = ""
             })
    }
}
于 2015-02-09T04:57:49.757 回答
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])));
        }

    }
}
于 2013-12-11T10:49:29.117 回答
0

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

扩展类和方法:

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;
    }
}

然后你只需要引用你的新类并调用@Html.TextBoxFor(m => m.SomeValue, new { @class = "someClass" }, <Your bool value>)

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

于 2015-08-27T18:46:37.510 回答