ASP.NET MVC3 - textarea with @Html.EditorFor

后端 未结 4 619
猫巷女王i
猫巷女王i 2020-12-12 10:31

I have ASP.NET MVC3 app and I have also form for add news. When VS2010 created default view I have only text inputs for string data, but I want to have textarea for news tex

相关标签:
4条回答
  • 2020-12-12 10:48

    You could use the [DataType] attribute on your view model like this:

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

    and then you could have a controller:

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

    and a view which does what you want:

    @model AppName.Models.MyViewModel
    @using (Html.BeginForm())
    {
        @Html.EditorFor(x => x.Text)
        <input type="submit" value="OK" />
    }
    
    0 讨论(0)
  • 2020-12-12 10:48

    Someone asked about adding attributes (specifically, 'rows' and 'cols'). If you're using Razor, you could just do this:

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

    That works for me. The '@' is used to escape keywords so they are treated as variables/properties.

    0 讨论(0)
  • 2020-12-12 10:49

    Declare in your Model with

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

    Then in .cshtml can make use of editor as below. you can make use of @cols and @rows for TextArea size

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

    Thanks !

    0 讨论(0)
  • 2020-12-12 11:02
    @Html.TextAreaFor(model => model.Text)
    
    0 讨论(0)
提交回复
热议问题