2012-01-09 33 views
9

嗨,人们我一直在这里为5天,并找不到解决方案,试图让这个去多线@Html.TextBoxFor(model => model.Headline, new { style = "width: 400px; Height: 200px;"})但我没有运气。TextBoxFor Mulitline

以下是我的尝试:

@Html.TextBoxFor.Multiline (does not work) 

我已经把多行上的新的结束和没有奏效。做这件事最简单的方法是什么?

谢谢我使用MVC3 C#

+0

可能的重复:http://stackoverflow.com/questions/6740890/mvc3-razor-html-textboxfor-can-i-make-it-multi-line-like-textareafor – rene 2012-01-09 20:44:16

+1

这是复杂的,为什么我想要这就是为什么我说“做这个最简单的方法是什么” – user1137472 2012-01-09 20:47:26

回答

46

你可以使用一个TextAreaFor帮手:

@Html.TextAreaFor(
    model => model.Headline, 
    new { style = "width: 400px; height: 200px;" } 
) 

而是一个更好的解决方案是与[DataType]属性指定要装饰你的Headline视图模型属性它呈现为:<textarea>

public class MyViewModel 
{ 
    [DataType(DataType.MultilineText)] 
    public string Headline { get; set; } 

    ... 
} 

然后使用EditorFor帮手:

<div class="headline"> 
    @Html.EditorFor(model => model.Headline) 
</div> 

终于在你的CSS文件中指定它的造型:

div.headline { 
    width: 400px; 
    height: 200px; 
} 

现在你有关切的妥善分离。

+2

textarea将成为你想要的一个多行表单字段。调整大小是发生在不同浏览器上的事情,Firefox和Chrome都会允许这种情况发生,因为IE不会。 – Chris 2012-01-09 20:49:24

+0

@Darin Thanks Darin为解释这件事情,在此之前我没有使用'[Datatype(DataType.MultilineText)]'注释为textarea谢谢。 – user1006544 2012-01-10 08:13:14

+2

我知道这是一个古老的帖子,但它是“剃刀多行textboxfor”的最高搜索结果,我只是想补充说CSS选择器是错误的,应该是'div.headline textarea' – Jocie 2016-02-10 09:31:54