How to size the TextArea and assign Model Value to it 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.
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.
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.
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.
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); }
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)
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" }) %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With