Reputation: 2134
In my view I have the following HTML, and yes I know I could use an HTML helper but in this case I cannot use that because of some scripting we use on the page causes serious problems when I allow MVC to create the textarea.
@Html.TextBox("name", "", new { @placeholder = "name", @class = "formElement", @id="name"})
<textarea id="message" placeholder="comments" class="formElement"></textarea>
Then in my Controller I have setup a very basic line to print out the name and message.
[HttpPost]
public ActionResult Form(MessageViewModel model)
{
return Content("Name: " + model.name + " Message: " + model.message);
}
Name is accessible just fine because it is created with the HTML helper, but I cant access message, nothing is ever returned for it. Both name and message are defined in my ViewModel:
public string name { get; set; }
public string message { get; set; }
How do I access the text that is in my message textarea?
Upvotes: 2
Views: 1977
Reputation: 398
In MVC ii is a good pratice to have a proper Model in a View and assign the Model properties to the desired controls such as the next example:
@Html.TextAreaFor(m => Model.remark, 10, 5, new { style = "resize: none" })
The @Html.TextAreaFor
will be rendered as:
<textarea cols="5" id="remark" name="remark" rows="10" style="resize: none"></textarea>
and will allow a correct posting when the form is submitted and the Action in the Controller is called with the Model sent by the View.
Upvotes: 1
Reputation: 17680
You should add a "name" attribute for the textarea. html elements are posted based on their names.
<textarea id="message" name="message" placeholder="comments" class="formElement"></textarea>
Upvotes: 6