Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to learn ASP.NET MVC4. Some if these concepts are new to me. My question today is:

How do I control the amount of rows and cols in a form when I use @Html.TextArea()

Here's my code if it helps, but it is a pretty general question

HTML
<div id="contact-form">
    @using(Html.BeginForm()) 
    {
        @Html.LabelFor(model => model.Name)<br />
        @Html.TextBoxFor(model => model.Name)<br />
        @Html.ValidationMessageFor(model => model.Name)<br />
    
        @Html.LabelFor(model => model.Phone)<br />
        @Html.TextBoxFor(model => model.Phone)<br />    
        @Html.ValidationMessageFor(model => model.Phone)<br />
    
        @Html.LabelFor(model => model.Email)<br />
        @Html.TextBoxFor(model => model.Email)<br />
        @Html.ValidationMessageFor(model => model.Email)<br />
    
        @Html.LabelFor(model => model.Comments)<br />
        @Html.TextAreaFor(model => model.Comments, new { rows= 10, columns= 40 })<br />
        @Html.ValidationMessageFor(model => model.Comments)<br />
    
        <button type="submit">SEND</button> 
        <button type="reset">CLEAR</button> 
    }
    
</div>

Thanks
Posted
Updated 21-May-13 6:56am
v3
Comments
kumar2413 21-May-13 20:46pm    
<pre lang="c#">@Html.TextAreaFor(model => model.Description, new { @class = "whatever-class", @cols = 80, @rows = 10 })</pre>

Try this and see

Wouldn't "rows" and "columns" suggest using a HTML table to you??
 
Share this answer
 
Comments
[no name] 21-May-13 12:03pm    
ok, let me rephrase...

How do I set rows, cols properties to the textarea of the form?

Sorry about that
Dave Kreskowiak 21-May-13 12:36pm    
You already have the answer in your code. Look at the TextArea you defined for Comments. The problem, I believe, is that the "rows" and "cols" must look like this:
@Html.TextAreaFor(model => model.Comments, new { @rows=10, @cols=40 })
[no name] 21-May-13 12:39pm    
I thought so. That's why I am here. It's not working for some reason. Thanks for looking anyway
Dave Kreskowiak 21-May-13 16:35pm    
I had to go dig up an old project, but the TextAreaFor method takes the parameters for row and col size for the resulting HTML textbox. The code should be:
@Html.TextAreaFor(model => model.Comments, 10, 40, null);
[no name] 21-May-13 16:41pm    
You got it Dave. That worked! The debugger gave me positive feedback on both cases, but style-wise, it took this one. Thanks
C#
@Html.TextAreaFor(model => model.Description, new { @class = "whatever-class", @cols = 80, @rows = 10 })


Try this and see
 
Share this answer
 
Comments
[no name] 22-May-13 10:10am    
Kumar, Dave's answer helped but your did too in other levels. I can see now how I can apply style classes within the @Html. tags. I was wondering about that. thanks
HTML
@Html.TextAreaFor(model => model.descricao, htmlAttributes: new { @class = "form-control", @rows= 5, @cols = 30 })
 
Share this answer
 
Comments
Member 10758790 3-Oct-19 2:45am    
This works perfect. width is adjusted as well as responsive

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900