C# 使用TextAreaFor禁用cols

C# 使用TextAreaFor禁用cols,c#,html,asp.net-mvc,twitter-bootstrap,C#,Html,Asp.net Mvc,Twitter Bootstrap,好的,我正在使用MVC4和引导,并且遇到了TextAreaFor和columns的问题。默认情况下,如果不设置列/行,则系统将保持2/10为默认值。显然,如果您输入行/列,它将使用该行/列。问题在于使用Bootstraper的表单,最好将其留空。任何人都能想出一个好方法来禁用TextAreaFor的col @Html.TextAreaFor(m=> m.Comments, new { htmlAttributes = new { @class = "form-control" } })

好的,我正在使用MVC4和引导,并且遇到了TextAreaFor和columns的问题。默认情况下,如果不设置列/行,则系统将保持2/10为默认值。显然,如果您输入行/列,它将使用该行/列。问题在于使用Bootstraper的表单,最好将其留空。任何人都能想出一个好方法来禁用TextAreaFor的col

@Html.TextAreaFor(m=> m.Comments, new { htmlAttributes = new { @class = "form-control" } })
产出:

<textarea cols="20" htmlAttributes="{ class = form-control }" id="Comments" name="Comments" rows="2"></textarea>

您可以为TextAreaFor设置行和列属性,默认情况下,它会添加cols=20和rows=2。也许你可以确切地说明你想要什么。举个例子

@Html.TextAreaFor(m => m.Commnets, new { @class = "whatever-class", @cols = 10 })

您可以为TextAreaFor设置行和列属性,默认情况下,它会添加cols=20和rows=2。也许你可以确切地说明你想要什么。举个例子

@Html.TextAreaFor(m => m.Commnets, new { @class = "whatever-class", @cols = 10 })

不幸的是,不可以,您不能关闭助手默认添加的属性;您只能更改其值。这主要是因为您不能在匿名对象中设置空项,也就是说,为
htmlAttributes
传递如下内容会引发异常:

new { cols = null }
你能做的唯一一件事就是把它从匿名对象中去掉,但是默认值会接管它。您可以尝试的一件事是:

new { cols = "" }
这将导致生成

<textarea cols="" ...>


但不确定这是否真的有效;你得测试一下。然而,我在我的项目中使用引导,我从来没有禁用过
cols
属性:它只是工作。您在引导过程中到底遇到了什么问题,因为这可能是可以解决的。

不幸的是,不,您无法关闭帮助程序默认添加的属性;您只能更改其值。这主要是因为您不能在匿名对象中设置空项,也就是说,为
htmlAttributes
传递如下内容会引发异常:

new { cols = null }
你能做的唯一一件事就是把它从匿名对象中去掉,但是默认值会接管它。您可以尝试的一件事是:

new { cols = "" }
这将导致生成

<textarea cols="" ...>


但不确定这是否真的有效;你得测试一下。然而,我在我的项目中使用引导,我从来没有禁用过
cols
属性:它只是工作。你用Bootstrap精确地遇到了什么问题,因为也许这是可以修复的。

< P>不幸的是,没有简单的解决方法(除非你能考虑JavaScript DOM操作)…您需要创建自己的HtmlHelper类以在视图中使用

将以下类添加到MVC项目中,并使用它。。。在视图中,您只需为(…)编写
@this.Html.textraeahelper,并以与使用
textraeafor
方法完全相同的方式使用它,但输出将不包括任何
属性


使用系统;
使用System.Collections.Generic;
使用System.Linq.Expressions;
使用System.Web;
使用global::System.Web.Mvc;
公共静态类TextAreaHelperExtension
{
公共静态MvcHtmlString TextAreaHelperFor(此HtmlHelper HtmlHelper,表达式)
{
返回TextAreaHelperFor(htmlHelper,表达式,null);
}
公共静态MvcHtmlString TextAreaHelperFor(此HtmlHelper HtmlHelper、表达式、对象htmlAttributes)
{
返回文本区域帮助器(htmlHelper、表达式、htmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
公共静态MvcHtmlString TextAreaHelperFor(此HtmlHelper HtmlHelper、表达式表达式、IDictionary htmlAttributes)
{
if(表达式==null)
{
抛出新的ArgumentNullException(“表达式”);
}
返回文本区域帮助器(htmlHelper,
ModelMetadata.FromLambdaExpression(表达式,htmlHelper.ViewData),
ExpressionHelper.GetExpressionText(表达式),
(三),;
}
内部静态MvcHtmlString TextAreaHelper(HtmlHelper、ModelMetadata ModelMetadata、字符串名称、IDictionary htmlAttributes、字符串innerHtmlPrefix=null)
{
字符串fullName=htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(名称);
if(String.IsNullOrEmpty(fullName))
{
抛出新ArgumentException();
}
标记生成器标记生成器=新标记生成器(“文本区域”);
tagBuilder.GenerateId(全名);
tagBuilder.MergeAttributes(htmlAttributes,true);
tagBuilder.MergeAttribute(“名称”,全名,true);
模型状态模型状态;
if(htmlHelper.ViewData.ModelState.TryGetValue(全名,out ModelState)&&ModelState.Errors.Count>0)
{
tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
}
tagBuilder.MergeAttributes(htmlHelper.GetUnobusiveValidationAttributes(名称、模型元数据));
字符串值;
if(modelState!=null&&modelState.Value!=null)
{
value=modelState.value.AttemptedValue;
}
else if(modelMetadata.Model!=null)
{
value=modelMetadata.Model.ToString();
}
其他的
{
value=String.Empty;
}
tagBuilder.InnerHtml=(innerHtmlPrefix??Environment.NewLine)+HttpUtility.HtmlEncode(值);
返回tagBuilder.ToMvcHtmlString(TagRenderMode.Normal);
}
内部静态MvcHtmlString ToMvcHtmlString(此TagBuilder TagBuilder,TagRenderMode renderMode)
{
返回新的MvcHtmlString(tagBuilder.ToString(renderMode));
}
}

不幸的是,没有简单的解决方法(除非你能考虑JavaScript DOM操作)…您需要创建自己的HtmlHelper类以在视图中使用

添加以下类别
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web;

using global::System.Web.Mvc;

public static class TextAreaHelperExtension
{
    public static MvcHtmlString TextAreaHelperFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
    {
        return TextAreaHelperFor(htmlHelper, expression, null);
    }

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

    public static MvcHtmlString TextAreaHelperFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
    {
        if (expression == null)
        {
            throw new ArgumentNullException("expression");
        }

        return TextAreaHelper(htmlHelper,
                                ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData),
                                ExpressionHelper.GetExpressionText(expression),
                                htmlAttributes);
    }

    internal static MvcHtmlString TextAreaHelper(HtmlHelper htmlHelper, ModelMetadata modelMetadata, string name, IDictionary<string, object> htmlAttributes, string innerHtmlPrefix = null)
    {
        string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
        if (String.IsNullOrEmpty(fullName))
        {
            throw new ArgumentException();
        }

        TagBuilder tagBuilder = new TagBuilder("textarea");
        tagBuilder.GenerateId(fullName);
        tagBuilder.MergeAttributes(htmlAttributes, true);
        tagBuilder.MergeAttribute("name", fullName, true);

        ModelState modelState;
        if (htmlHelper.ViewData.ModelState.TryGetValue(fullName, out modelState) && modelState.Errors.Count > 0)
        {
            tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
        }

        tagBuilder.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name, modelMetadata));

        string value;
        if (modelState != null && modelState.Value != null)
        {
            value = modelState.Value.AttemptedValue;
        }
        else if (modelMetadata.Model != null)
        {
            value = modelMetadata.Model.ToString();
        }
        else
        {
            value = String.Empty;
        }

        tagBuilder.InnerHtml = (innerHtmlPrefix ?? Environment.NewLine) + HttpUtility.HtmlEncode(value);

        return tagBuilder.ToMvcHtmlString(TagRenderMode.Normal);
    }

    internal static MvcHtmlString ToMvcHtmlString(this TagBuilder tagBuilder, TagRenderMode renderMode)
    {
        return new MvcHtmlString(tagBuilder.ToString(renderMode));
    }
}
@Html.TextAreaFor(m => m.Comments, 0, 0, new { @class="form-control" })
<textarea class="form-control" id="Comments" name="Comments"></textarea>