Asp.net mvc 3 为TextBoxFor HtmlHelper动态设置禁用的html属性

Asp.net mvc 3 为TextBoxFor HtmlHelper动态设置禁用的html属性,asp.net-mvc-3,Asp.net Mvc 3,我正在尝试为TextBoxForHtmlHelper动态设置disabled属性 @Html.TextBoxFor(model => model.Street, new { @class = "", disabled = (Model.StageID==(int)MyEnum.Sth) ? "disabled" : ""

我正在尝试为
TextBoxFor
HtmlHelper动态设置
disabled
属性

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

但是,即使存在
disabled=”“
,它也与
disabled=“disabled”
相同。如何解决这个问题?

大约一个月前,我遇到了同样的问题,我使用这个扩展方法完成了这个问题

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)
)
编辑(在s之后):

可以使用类的构造函数来挖掘
data xxx
html属性,因为下划线不会自动转换为减号

使用以下方法:将解决此问题

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


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

@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])));
        }

    }
}
使用System.Text.regular表达式;
使用System.Web.Mvc;
命名空间xxx.HtmlHelpers
{
公共静态类MvcHtmlStringExtensions
{
私有静态只读Regex OpeningTagPattern;
静态MvcHtmlStringExtensions()
{

OpeningTagPattern=newregex(“可能您的阶段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 = ""
             })
    }
}

我们实际上遇到了同样的问题。我们最终实现了一个带有重载参数的扩展方法,该方法使用一个布尔值来指示是否要禁用控件。我们只是在适当的时候添加“disabled”属性,并让内置的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;
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq.Expressions;
使用System.Web.Mvc;
使用System.Web.Mvc.Html;
使用System.Web.Routing;
公共静态类OurHtmlHelpers
{
public const string DisabledAttribute=“disabled”;
公共静态MvcHtmlString TextBoxFor(此HtmlHelper HtmlHelper,
表情表情,
对象属性,
布尔卡内特)
{
var HtmlAttributedDictionary=SetDisabledAttribute(htmlAttributes,canEdit);
返回htmlHelper.TextBoxFor(表达式,HtmlAttributedDictionary);
}        
私有静态RouteValueDictionary SetDisabledAttribute(对象htmlAttributes,bool canEdit)
{
var HtmlAttributedDictionary=新路由值字典(htmlAttributes);
如果(!canEdit)
{
添加(DisabledAttribute,DisabledAttribute);
}
返回HTMLATTRIBUTEDICTIONAL;
}
}
然后您只需要引用新类并调用
@Html.TextBoxFor(m=>m.SomeValue,new{@class=“someClass”},)


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

这很厚颜无耻:>但我喜欢。不过,我添加了一个(更脆弱的)我自己的变体接受的答案中有一个问题。您应该调用HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes),而不是新的RouteValueDictionary(htmlAttributes)HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)将html属性名称中的所有下划线转换为连字符。感谢@ChuckNorris和Paul的更正。我刚刚编辑了答案。我在这里发现扩展的即用版本窃取了我的剑道UI dynamic readonly:)我正在将我的代码切换到上面Chuck Norris的版本:)
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;
    }
}