Reputation: 1
I need to set data into a textarea in asp.net mvc razor page (.cshtml) from controller in C#. My code snippet as following: I am not able to assign data into model.
In cshtml:
@Html.TextBoxFor(b => b.query, new { Value = ViewBag.query, height = "500px;" })
In controller:
ViewBag.query = "Hello";
model.query = ViewBag.query;
In Model:
[Display(Name = "Query")]
public string query { get; set; }
Upvotes: 0
Views: 1269
Reputation: 488
I don't see any reason why you should use the Value parameter when the value for the field has been assigned in the controller. I am not sure why this is not working because you didn't provide the whole code so i'll just try to fill in the blanks.
Model:
public class ModelName
{
[Display(Name = "Query")]
public string query { get; set; }
}
In the controller, make sure you return the model.
public class ControllerName : Controller
{
public ActionResult ActionName()
{
ViewBag.query = "Hello";
var model = new ModelName
{
query = ViewBag.query
}
return View(model)
}
}
In the view, make sure the object type is referenced on the page...
@model ApplicationName.Models.ModelName
So if anywhere in the view, you can get the value for the text area this way.
@Html.TextAreaFor(model => model.query, new { @class = "form-control", placeholder = "Add Placeholder", id = "textarea", autocomplete = "off", @rows = "10", style = "width: 70%; max-width: 100%;" }
You can always change the style or don't use any at all depending on what you want.
Upvotes: 1
Reputation: 1554
You can define multiline textbox in following way :
@Html.TextAreaFor(x => x.Body, 10, 15, null)
Upvotes: 0