MVC3 HTML Helper for large Text area

17,623

Solution 1

Try

Html.TextAreaFor(model => model.Description, new {@cols="80" , @rows="4" })

Solution 2

Use:

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

// or a full option-list is:
@Html.TextAreaFor(model => model.Description, 
    rows, // the rows attribute of textarea for example: 4
    columns, // the cols attribute of textarea for example: 40
    new { }) // htmlAttributes to add to textarea for example: @class = "my-css-class"

Notice: you can use null instead of new { } for htmlAttributes but it is not recommended! It's strongly recommended that use a blank new { } -that represents a new object-

Solution 3

You can use EditorFor, but in that case it's better to define your own EditorTemplate for rendering your TextArea, using TextAreaFor or whatever it's needed.

The main difference between the TextAreaFor and EditorFor is that, if I've understood well how everything works, when using EditorFor, Templates are taken into account, while when using TextAreaFor you choose the HTML Input used for rendering.

Templates seems interesting, I'm just starting digging into writing my own.

Share:
17,623
MikeTWebb
Author by

MikeTWebb

I am a C# .Net/SQL/Oracle/Web developer. Also experienced in Business Analysis and Customer Interaction.

Updated on June 16, 2022

Comments

  • MikeTWebb
    MikeTWebb almost 2 years

    I have an html helper:

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

    But it is too small for the data in that property of my Model. Descriptino is a 1000 character string. I need the user to be able to enter several lines of text and have it wrap in the HTML object. How do I do this?

  • amiry jd
    amiry jd over 12 years
    A great answer for a simple question. Cool and perfect