Asp.net mvc Asp.net MVC文本区
如何在Asp.net Mvc中调整TextArea的大小并为其分配模型值假定您有某个模型类的强类型视图,您可以使用以下方法:Asp.net mvc Asp.net MVC文本区,asp.net-mvc,Asp.net Mvc,如何在Asp.net Mvc中调整TextArea的大小并为其分配模型值假定您有某个模型类的强类型视图,您可以使用以下方法: <%= Html.TextAreaFor(x => x.SomeProperty, new { rows = "20", cols = "10" }) %> x.SomeProperty,新的{rows=“20”,cols=“10”})%> 或: x.SomeProperty,20,10,new{@class=“foo”})%> 试试这个: <
<%= Html.TextAreaFor(x => x.SomeProperty, new { rows = "20", cols = "10" }) %>
x.SomeProperty,新的{rows=“20”,cols=“10”})%>
或:
x.SomeProperty,20,10,new{@class=“foo”})%>
试试这个:
<%=Html.TextAreaFor(
m => m.Description, 15, 20,
new RouteValueDictionary(new { @class = "someClass"}))%>
m.描述,15,20,
新建RouteValueDictionary(新建{@class=“someClass”}))%>
编辑:据我所知,这行不通
<%=Html.TextAreaFor(m => m.Description, new { cols = "20", rows = "15" })%>
m.说明,新的{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);
}
private const int TextAreaRows=2;
private const int TextAreaColumns=20;
// ...
公共静态字符串文本区(
此HtmlHelper HtmlHelper,字符串名称,
I词典(赠品){
字典隐式属性=新字典();
implicitAttributes.Add(“rows”,TextAreaRows.ToString(CultureInfo.InvariantCulture));
implicitAttributes.Add(“cols”,TextAreaColumns.ToString(CultureInfo.InvariantCulture));
返回TextAreaHelper(htmlHelper,name,true/*useViewData*/,null/*value*/,implicitAttributes,null/*explicitParameters*/,htmlAttributes);
}
陷阱是@Html.TextAreaFor
,因为它没有允许您分配模型值的重载
例1:
示例1不会引发异常,也不会显示任何文本。放轻松
解决方案:
改用@Html.TextArea
例2:
建议:
[DataType(DataType.MultilineText)]
public string Comments { get; set; }
@Html.EditorFor(model => model.Comments)
您也应该让Aspx失望,因为Razor更轻,语法等效
只需使用@
而不是
我找到了一个简单的方法来实现这一点
使用models annotation razor将非常智能,可以生成textarea
型号:
[DataType(DataType.MultilineText)]
public string Comments { get; set; }
@Html.EditorFor(model => model.Comments)
查看:
[DataType(DataType.MultilineText)]
public string Comments { get; set; }
@Html.EditorFor(model => model.Comments)
对于您的第二个示例:x.SomeProperty,20,10,null)%%>对于您的第一个示例:m.Description,15,20,null)%%>添加或删除字典都无所谓=)我认为问题作者可以处理这个=)