简体   繁体   English

使用Html.EditorFor生成具有特定行数和列数的textarea

[英]Using Html.EditorFor to generate a textarea with a specific number of rows and columns

I know you can use the DataType attribute with the EditorFor html helper to specify that a particular property of a model entity should be displayed as a multi-line input field. 我知道您可以将DataType属性与EditorFor html帮助器一起使用,以指定模型实体的特定属性应显示为多行输入字段。

What If I want to specify the number of rows and columns the text area must have? 如果我想指定文本区域必须包含的行数和列数,该怎么办?

In the model : 在模型中:

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

In the view : 在视图中:

@Html.EditorFor(x => x.HTMLText)

Wanted result : 通缉结果:

<textarea id="HTMLText" rows="10" cols="40">value</textarea>

Is there a way to generate this kind of code without using the @Html.Textarea() helper? 有没有办法在不使用@ Html.Textarea()助手的情况下生成这种代码?

Not sure how to set the rows and cols, but you can alter the CSS of those textareas using the .multi-line class. 不确定如何设置行和列,但您可以使用.multi-line类更改这些textareas的CSS。 This class is added to the textarea when using EditorFor so you can specify the width and height in that class to get your desired dimensions. 使用EditorFor时,此类将添加到textarea中,以便您可以指定该类中的宽度和高度以获得所需的尺寸。

.multi-line { height:5em; width:5em; }

你可以在Razor / c#中指定row和col的属性大小和char等价物,即:

@Html.TextAreaFor(model => model.Longtext, new { htmlAttributes = new { @class = "ctrl-col2 ctrl-col1", @row = "4", @col = "30" } })

当您使用带有DataType.MultilineText字段的@ Html.EditorFor时,MVC 5上不再需要这样做

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM