如何在 ASP.MVC 中指定多行 Editor-For 的列和行?

发布于 2024-11-13 01:33:03 字数 718 浏览 4 评论 0原文

在 ASP.MVC 3 中,如何指定多行 EditorFor (文本区域)的列和行?我正在使用 [UIHint("MultilineText")],但找不到任何有关如何为文本区域添加属性的文档。

所需的 HTML:

 <textarea cols="40" rows="10"></textarea>

我的 MVC 3 模型的相关部分:

[UIHint("MultilineText")]
public string Description { get; set; }

我的 Razor cshtml 的相关部分:

<div class="editor-field">
    @Html.EditorFor(model => model.Description)
</div>

我在查看源代码中得到的内容:

 <div class="editor-field">
     <textarea class="text-box multi-line" id="Description" name="Description"></textarea>
 </div>

如何设置行和列?

In ASP.MVC 3, how do I specify the columns and rows for a multiline EditorFor (textarea)? I am using [UIHint("MultilineText")], but can't find any documentation on how to add attributes for the text area.

Desired HTML:

 <textarea cols="40" rows="10"></textarea>

Relevant Part of my MVC 3 Model:

[UIHint("MultilineText")]
public string Description { get; set; }

Relevant Part of my Razor cshtml:

<div class="editor-field">
    @Html.EditorFor(model => model.Description)
</div>

What I'm getting in View Source:

 <div class="editor-field">
     <textarea class="text-box multi-line" id="Description" name="Description"></textarea>
 </div>

How do I set rows and columns?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

伴我老 2024-11-20 01:33:03

使用 TextAreaFor

@Html.TextAreaFor(model => model.Description, new { @class = "whatever-class", @cols = 80, @rows = 10 })

或使用 multi-line 类的样式。

您还可以为此编写 EditorTemplate

Use TextAreaFor

@Html.TextAreaFor(model => model.Description, new { @class = "whatever-class", @cols = 80, @rows = 10 })

or use style for multi-line class.

You could also write EditorTemplate for this.

坐在坟头思考人生 2024-11-20 01:33:03

在 ASP.NET MVC 5 中,您可以使用 [DataType(DataType.MultilineText)] 属性。它将呈现一个 TextArea 标记。

public class MyModel
{
    [DataType(DataType.MultilineText)]
    public string MyField { get; set; }
}

然后在视图中,如果您需要指定行,您可以这样做:

@Html.EditorFor(model => model.MyField, new { htmlAttributes = new { rows = 10 } })

或者只需使用具有正确重载的 TextAreaFor :

@Html.TextAreaFor(model => model.MyField, 10, 20, null)

In ASP.NET MVC 5 you could use the [DataType(DataType.MultilineText)] attribute. It will render a TextArea tag.

public class MyModel
{
    [DataType(DataType.MultilineText)]
    public string MyField { get; set; }
}

Then in the view if you need to specify the rows you can do it like this:

@Html.EditorFor(model => model.MyField, new { htmlAttributes = new { rows = 10 } })

Or just use the TextAreaFor with the right overload:

@Html.TextAreaFor(model => model.MyField, 10, 20, null)
乖乖 2024-11-20 01:33:03

我相信这个也可以更轻松地使用(但我使用的是 MVC 5)

@Html.Description(model => model.Story, 20, 50, new { })

在此处输入图像描述

This one can also be used with less effort I believe (but I am in MVC 5)

@Html.Description(model => model.Story, 20, 50, new { })

enter image description here

陌路终见情 2024-11-20 01:33:03

一种选择似乎是使用 CSS 来设置文本区域的样式

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

请参阅此条目 或 这个。

Amurra 接受的答案似乎暗示在使用 EditorFor 时会自动添加此类,但您必须验证这一点。

编辑:已确认,确实如此。所以,是的,如果您想使用 EditorFor,使用此 CSS 样式即可满足您的要求。

<textarea class="text-box multi-line" id="StoreSearchCriteria_Location" name="StoreSearchCriteria.Location">

One option seems to be using CSS to style the textarea

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

See this entry on SO or this one.

Amurra's accepted answer seems to imply this class is added automatically when using EditorFor but you'd have to verify this.

EDIT: Confirmed, it does. So yes, if you want to use EditorFor, using this CSS style does what you're looking for.

<textarea class="text-box multi-line" id="StoreSearchCriteria_Location" name="StoreSearchCriteria.Location">
懒的傷心 2024-11-20 01:33:03

在MVC 5中

              @Html.EditorFor(x => x.Address, 
              new {htmlAttributes = new {@class = "form-control", 
                   @placeholder = "Complete Address", @cols = 10, @rows = 10 } })

in mvc 5

              @Html.EditorFor(x => x.Address, 
              new {htmlAttributes = new {@class = "form-control", 
                   @placeholder = "Complete Address", @cols = 10, @rows = 10 } })
情绪操控生活 2024-11-20 01:33:03

.net VB 中 - 您可以在 razor 文件中使用以下内容来实现对列和行的控制:

@Html.EditorFor(Function(model) model.generalNotes, New With {.htmlAttributes = New With {.class = "someClassIfYouWant", .rows = 5,.cols=6}})

In .net VB - you could achieve control over columns and rows with the following in your razor file:

@Html.EditorFor(Function(model) model.generalNotes, New With {.htmlAttributes = New With {.class = "someClassIfYouWant", .rows = 5,.cols=6}})
顾忌 2024-11-20 01:33:03

@Html.TextArea("txtNotes", "", 4, 0, new { @class = "k-textbox", style = "宽度: 100%; 高度: 100%;" })

@Html.TextArea("txtNotes", "", 4, 0, new { @class = "k-textbox", style = "width: 100%; height: 100%;" })

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文