Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net MVC TextArea

Tags:

asp.net-mvc

How to size the TextArea and assign Model Value to it in Asp.net Mvc

like image 960
yogeswaran K Avatar asked Dec 17 '10 09:12

yogeswaran K


People also ask

How to add TextArea in ASP net MVC?

Create TextArea in ASP.NET MVC The HtmlHelper class includes two extension methods to render multi-line <textarea> HTML control in a razor view: TextArea() and TextAreaFor<TModel, TProperty>() . By default, it creates a textarea with rows=2 and cols=20.

What is HTML TextAreaFor?

TextAreaFor<TModel,TProperty>(HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>, Int32, Int32, Object) Returns an HTML textarea element for each property in the object that is represented by the specified expression using the specified HTML attributes and the number of rows and columns.

What is ModelState MVC?

ModelState is a property of a Controller instance, and can be accessed from any class that inherits from Microsoft. AspNetCore. Mvc. Controller. The ModelState has two purposes: to store and submit POSTed name-value pairs, and to store the validation errors associated with each value.

What is HTML raw in MVC?

The Html. Raw Helper Method is used to display HTML in Raw format i.e. without encoding in ASP.Net MVC Razor. Configuring Bundles. Please refer the following article for complete information on how to configure Bundles in ASP.Net MVC project. Using Bundles (ScriptBundle) in ASP.Net MVC Razor.


3 Answers

Try this:

 <%=Html.TextAreaFor(         m => m.Description, 15, 20,          new RouteValueDictionary(new { @class = "someClass"}))%> 

Edit:
This wont work as far as I know

<%=Html.TextAreaFor(m => m.Description, new { cols = "20", rows = "15" })%> 

because of this:

private const int TextAreaRows = 2; private const int TextAreaColumns = 20;  // ...           public static string TextArea(                 this HtmlHelper htmlHelper, string name,                  IDictionary<string, object> htmlAttributes) {             Dictionary<string, object> implicitAttributes = new Dictionary<string, object>();             implicitAttributes.Add("rows", TextAreaRows.ToString(CultureInfo.InvariantCulture));             implicitAttributes.Add("cols", TextAreaColumns.ToString(CultureInfo.InvariantCulture));             return TextAreaHelper(htmlHelper, name, true /* useViewData */, null /* value */, implicitAttributes, null /* explicitParameters */, htmlAttributes);  } 
like image 179
Aleksei Anufriev Avatar answered Sep 23 '22 11:09

Aleksei Anufriev


I found a simple away to achieve this.

Using models annotation razor will be smart enough to generate the textarea.

Model:

[DataType(DataType.MultilineText)] public string Comments { get; set; } 

View:

@Html.EditorFor(model => model.Comments) 
like image 34
meda Avatar answered Sep 20 '22 11:09

meda


Assuming you have a strongly typed view to some model class you could use the following:

<%= Html.TextAreaFor(x => x.SomeProperty, new { rows = "20", cols = "10" }) %>

or:

<%= Html.TextAreaFor(x => x.SomeProperty, 20, 10, new { @class = "foo" }) %>
like image 24
Darin Dimitrov Avatar answered Sep 21 '22 11:09

Darin Dimitrov