C# 多行文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8794906/
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
TextBoxFor Mulitline
提问by user1137472
Hi people I have been at this for like 5 days and could not find a solution am trying to get this to go on multi line @Html.TextBoxFor(model => model.Headline, new { style = "width: 400px; Height: 200px;"})but I have had no luck.
大家好,我已经在这里待了大约 5 天,但找不到解决方案,我试图让它在多线上运行,@Html.TextBoxFor(model => model.Headline, new { style = "width: 400px; Height: 200px;"})但我没有运气。
The following is what I tried:
以下是我尝试过的:
@Html.TextBoxFor.Multiline (does not work)
I have put Multiline on the end of new and that has not worked. What is the simplest way of doing this.
我已将 Multiline 放在 new 的末尾,但没有奏效。这样做的最简单方法是什么。
Thank You I am using MVC3 C#
谢谢我正在使用 MVC3 C#
采纳答案by Darin Dimitrov
You could use a TextAreaForhelper:
您可以使用TextAreaFor助手:
@Html.TextAreaFor(
model => model.Headline,
new { style = "width: 400px; height: 200px;" }
)
but a much better solution is to decorate your Headlineview model property with the [DataType]attribute specifying that you want it to render as a <textarea>:
但更好的解决方案是Headline使用[DataType]指定您希望它呈现为的属性来装饰您的视图模型属性<textarea>:
public class MyViewModel
{
[DataType(DataType.MultilineText)]
public string Headline { get; set; }
...
}
and then use the EditorForhelper:
然后使用EditorFor助手:
<div class="headline">
@Html.EditorFor(model => model.Headline)
</div>
and finally in your CSS file specify its styling:
最后在您的 CSS 文件中指定其样式:
div.headline {
width: 400px;
height: 200px;
}
Now you have a proper separation of concerns.
现在你有一个适当的关注点分离。

