asp.net-mvc Asp.net MVC TextArea

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4469277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:38:50  来源:igfitidea点击:

Asp.net MVC TextArea

asp.net-mvc

提问by yogeswaran K

How to size the TextArea and assign Model Value to it in Asp.net Mvc

如何在 Asp.net Mvc 中调整 TextArea 的大小并为其分配模型值

回答by Aleksei Anufriev

Try this:

尝试这个:

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

Edit:
This wont work as far as I know

编辑:
据我所知,这行不通

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

because of this:

因为这:

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

}

回答by meda

I found a simple away to achieve this.

我找到了一个简单的方法来实现这一目标。

Using models annotation razor will be smart enough to generate the textarea.

使用模型注释剃刀将足够智能以生成textarea.

Model:

模型:

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

View:

看法:

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

回答by Darin Dimitrov

Assuming you have a strongly typed view to some model class you could use the following:

假设您有某个模型类的强类型视图,您可以使用以下内容:

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

or:

或者:

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

回答by Francois Rossello

Pitfallis @Html.TextAreaForbecause it has no overload which allow you assigning a Model Value.

陷阱@Html.TextAreaFor因为它没有允许您分配模型值的过载。

Example 1:

示例 1

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

Example 1 wont raise exception and wont show any text. Let it down.

示例 1 不会引发异常并且不会显示任何文本。放下它。

Solution:

解决方案

use @Html.TextAreainstead

使用@Html.TextArea替代

Example 2:

示例 2:

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

Advice :

建议 :

You should let down Aspx too because Razor is lighter and equivalent syntax.

您也应该放弃 Aspx,因为 Razor 更轻巧且语法相同。

Just use @instead of <%= %>.

只需使用@代替<%= %>.