How to Generate Multiline TextBox or TextArea with @Html.EditorFor? | ASP.NET MVC Interview Question

,

Multiline TextBox or TextArea with @Html.EditorFor can be achieved in ASP.NET MVC by decorating your field by [DataType] attribute in your view model as given below

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

Then, pass the ViewModel to your view

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

The below view with @Html.EditorFor will render a Multiline TextBox or TextArea in ASP.NET MVC Razor view

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