212

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 text. How I can do it with Razor syntax.

Text input look like this:

@Html.EditorFor(model => model.Text)
Jacob Jedryszek
  • 6,365
  • 10
  • 34
  • 39
  • Related, see [this answer](http://stackoverflow.com/a/10696647/419956) to another question about how to customize that EditorTemplate. – Jeroen Feb 22 '15 at 14:04

4 Answers4

383

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" />
}
Tallmaris
  • 7,605
  • 3
  • 28
  • 58
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
139

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.

Tyson Phalp
  • 2,737
  • 3
  • 20
  • 12
  • Indeed - if you know your want a text area with cols/rows, there is little reason to use EditorFor instead of TextAreaFor. Anyone have a reason you'd still need to use EditorFor and know you need to specify cols/rows? – James Haug Oct 26 '16 at 17:50
98
@Html.TextAreaFor(model => model.Text)
animuson
  • 53,861
  • 28
  • 137
  • 147
addy
  • 1,215
  • 1
  • 9
  • 11
  • 6
    I like this method better because the popular answer here involves modifying the database model, which means you have to drop and recreate the underlying database if using EntityFramework. – Ciaran Gallagher Mar 28 '13 at 18:13
  • 6
    That DataType Annotation does not force a refresh in Entity Framework. – Tallmaris Dec 17 '13 at 17:09
  • 10
    @Ciaran: This statement should ring a bell. There should never be any need to change the database layer to modify the UI. There should be a presentation object, which is mapper to the database object. Never use the database object in ur UI. – Frederik Prijck Jul 08 '14 at 07:55
  • 5
    To be clear, what Frederik is referring to is creating classes that represent your view data SEPARATE from classes that are used in your DbContext. Don't pass your DbContext models into views. Create a view model class, then shift the info you care about from the db model into the view model, and vice versa when accepting inputs. – Jim Yarbro Jul 20 '14 at 19:26
  • 3
    @FrederikPrijck I don't disagree, but doesn't that violate the DRY principal? You have to copy all the properties from one class to another class. Is there a less "mundane" of doing it, that you have found? – James Haug Oct 26 '16 at 17:52
  • @JamesHaug The cons of this duplication are outweighed by its pros. If mapping is your concern, there are tools like AutoMapper that can help. – OJ Raqueño Oct 27 '20 at 01:22
4

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 !

Abdul Hadee
  • 113
  • 6