ChatGPT解决这个技术问题 Extra ChatGPT

ASP.NET MVC3 - textarea with @Html.EditorFor

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)
Related, see this answer to another question about how to customize that EditorTemplate.

T
Tallmaris

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" />
}

Pretty much what I was looking for, but what if I need to specify the rows and cols html attributes?
I am still getting [class="text-box single-line"] at the source code :(
Extremely knowledgeable and still keeping it as simple as it gets and explains everything step by step. hail @Darin Dimitrov.
@Jason use CSS for styling.
DataAnnotations ftw! Thank you.
T
Tyson Phalp

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.


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?
a
animuson
@Html.TextAreaFor(model => model.Text)

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.
That DataType Annotation does not force a refresh in Entity Framework.
@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.
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.
@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?
A
Abdul Hadee

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 !