ASP.NET MVC3 - 带@ Html.EditorFor的textarea

时间:2011-02-07 21:46:36

标签: asp.net-mvc-3

我有ASP.NET MVC3应用程序,我也有表单添加新闻。当VS2010创建默认视图时,我只有字符串数据的文本输入,但我想要textarea用于新闻文本。我如何使用Razor语法来实现它。

文字输入如下:

@Html.EditorFor(model => model.Text)

4 个答案:

答案 0 :(得分:367)

您可以在视图模型上使用[DataType]属性,如下所示:

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

然后你可以有一个控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }
}

和一个符合你想要的观点:

@model AppName.Models.MyViewModel
@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.Text)
    <input type="submit" value="OK" />
}

答案 1 :(得分:132)

有人询问添加属性(特别是'行'和'cols')。如果你正在使用Razor,你可以这样做:

@Html.TextAreaFor(model => model.Text, new { cols = 35, @rows = 3 })

这对我有用。 '@'用于转义关键字,因此它们被视为变量/属性。

答案 2 :(得分:95)

@Html.TextAreaFor(model => model.Text)

答案 3 :(得分:2)

使用

在模型中声明
  [DataType(DataType.MultilineText)]
  public string urString { get; set; }

然后,.cshtml中的代码可以如下使用编辑器。您可以使用 @cols @rows 来获取TextArea大小

     @Html.EditorFor(model => model.urString, new { htmlAttributes = new { @class = "",@cols = 35, @rows = 3 } })

谢谢!

相关问题