2014-09-22 48 views
0

好的我正在使用MVC 4與Bootstrap並運行TextAreaFor和列的問題。默認情況下,如果您未設置列/行,則系統將默認爲2/10。很明顯,如果你輸入行/列將會使用它。問題出在使用Bootstraper的表單上,如果你只留下空白就更好。任何人都想出了禁用TextAreaFor的col的好方法。用TextAreaFor禁用cols

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

,但我想這一點:

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

這工作,但並不友好:

<textarea id="Comments" name="Comments" rows="10" class="form-control">@Model.Comments</textarea> 

編輯 - 哦我創建了一個自定義編輯器模板,例如:

@{ 
string id = ViewData.ModelMetadata.PropertyName; 
string rows = string.Empty; 
if (ViewData["rows"] != null) 
{ 
    rows = string.Format("rows={0}", ViewData["rows"]); 
} 
} 

<textarea id="@id" name="@id" @rows class="form-control">@Model</textarea> 

用法:

@Html.EditorFor(model => model.Comments, "TextAreaCustom", new { @rows = 10 }) 

回答

0

有行和列的屬性,你可以設置TextAreaFor,它在默認情況下,它看起來像添加cols = 20和rows = 2。也許你可以指定你想要的。比如像這樣的東西。

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

不幸的是,你不能關閉helper默認添加的屬性;你只能改變它的價值。這主要是由於這樣的事實,你不能在一個匿名對象設置爲空項,即通過類似的htmlAttributes下會引發異常:

new { cols = null } 

你能做的唯一的事情,就是把它在匿名對象之外,但是默認接管。有一兩件事你可以嘗試做:

new { cols = "" } 

這將導致的

<textarea cols="" ...> 

不知道這將實際工作,雖然生成的HTML;你必須測試它。但是,我在我的項目中使用Bootstrap,並且我從不必禁用cols屬性:它只是起作用。你在使用Bootstrap時遇到了什麼問題,因爲這可能是可以修復的原因。

+0

當山坳屬性不存在自舉將文本區域設置爲它所在div的100%。顯然,當col被設置時,它就是這個寬度,所以如果他改變大小,或者該框看起來太小或比屏幕大。我試圖將所有表單設置爲基於瀏覽器大小使用xs/sm/md大小。 – 2014-09-22 20:10:10

+1

這是我的觀點:Bootstrap樣式覆蓋了屬性,因爲它們爲textarea設置了明確的寬度。如果這沒有發生,那麼Bootstrap樣式就會以某種方式頂起來。假設textarea具有必要的「表格控制」類,無論「cols」設置爲什麼或者甚至設置了什麼,寬度都應該是容器的100%。 – 2014-09-22 21:41:26

0

不幸的是,這沒有簡單的解決(除非你可以考慮JavaScript的DOM操作)......你需要創建自己的HtmlHelper類添加到你的觀點用...

添加下面的類到您的MVC項目,並使用它...你的意見,你可以簡單地寫@this.Html.TextAreaHelperFor(...),並使用它,你會使用TextAreaFor方法完全相同的方式,除了輸出不會被包括任何colrow屬性


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