2010-12-17 6 views
30

どのサイズのTextAreaをすると、あなたが以下のように使用することができ、いくつかのモデルクラスに強く型付けされたビューを持っていると仮定するとAsp.net MVCでそれにAsp.net MVCのTextArea

答えて

33

これを試してみてください:

<%=Html.TextAreaFor(
     m => m.Description, 15, 20, 
     new RouteValueDictionary(new { @class = "someClass"}))%> 

編集:
限り、私はこのための

<%=Html.TextAreaFor(m => m.Description, new { cols = "20", rows = "15" })%> 

知っているように、この文句を言わない仕事:それはあなたがモデル値を割り当てることができ過負荷を持っていないので、

private const int TextAreaRows = 2; 
private const int TextAreaColumns = 20; 

// ... 





    public static string TextArea(
       this HtmlHelper htmlHelper, string name, 
       IDictionary<string, object> htmlAttributes) { 
      Dictionary<string, object> implicitAttributes = new Dictionary<string, object>(); 
      implicitAttributes.Add("rows", TextAreaRows.ToString(CultureInfo.InvariantCulture)); 
      implicitAttributes.Add("cols", TextAreaColumns.ToString(CultureInfo.InvariantCulture)); 
      return TextAreaHelper(htmlHelper, name, true /* useViewData */, null /* value */, implicitAttributes, null /* explicitParameters */, htmlAttributes); 

} 
+0

あなたの最初の例:%= Html.TextAreaFor(m => m.Description、15、20、null)%> –

+0

辞書を追加または削除しても問題ありません= ) –

9

をモデル値を割り当てる:

<%= Html.TextAreaFor(x => x.SomeProperty, new { rows = "20", cols = "10" }) %> 

か:

<%= Html.TextAreaFor(x => x.SomeProperty, 20, 10, new { @class = "foo" }) %> 
+0

<%= Html.TextAreaFor(X => x.SomeProperty、20、10、NULL) %> –

1

落とし穴@Html.TextAreaForです。

例1

@Html.TextAreaFor(m => m.Language, 6, 40, new { @class = "form-control",@value="Tft.WebRole.Properties.Settings.Default.DefaultLanguage"} 

例1文句を言わない例外を発生し、文句を言わない任意のテキストを表示します。それを放棄しなさい。

ソリューション

使用@Html.TextArea代わり

例2:

@Html.TextArea("Language", Tft.WebRole.Properties.Settings.Default.DefaultLanguage, 6, 40, new { @class = "form-control" }) 

アドバイス:

レイザーので、あなたがあまりにもASPXを失望させなければなりません軽量で同等の構文です。

ちょうど私がこれを達成するために、単純な離れた@代わりの<%= %>.

22

を使用しています。

モデルの使用注釈剃刀は、textareaを生成するのに十分スマートです。

モデル:

[DataType(DataType.MultilineText)] 
public string Comments { get; set; } 

表示:あなたの第2例

@Html.EditorFor(model => model.Comments)