Reputation: 2803
Im new at Asp.Net MVC3, and i was trying to use CKEditor. But can't get my typed text then i push submit.
My View:
<form method=post action="@Url.Action("Description")">
<textarea class="ckeditor" id="editor1" rows="10" name="Details">@Resources.Resources.DescriptionSampleText</textarea>
<input type="submit" />
</form>
And the controller there i need the text:
[HttpPost]
public ActionResult Description(string textdetails)
{
//Doing something with the text
return RedirectToAction("Create", "Project", new { text = textdetails});
}
What am I doing wrong?
Upvotes: 1
Views: 2447
Reputation: 7517
There are three solutions to your problem. I will start of with solving it directly (two ways), however, in my opinion it is not the best way. Anyway, more about that later.
ASP.NET MVC (3) works a lot convention-based. It will magically assign values etc. from requests to parameters and other. Of course, these conventions are obviously based upon the names of your parameters. You'll have to make sure that your names match (as you might figure out right now, this will be a pain-in-the-a to maintain).
The quick solution is to name your textarea in your view the same as your parameter of your HttpPost action. Your view code would look like this:
<form method=post action="@Url.Action("Description")">
<textarea class="ckeditor" id="editor1" rows="10" name="Textdetails">@Resources.Resources.DescriptionSampleText</textarea>
<input type="submit" />
</form>
This should work. Note: I didn't test this myself right now, however many beginners guides do this as well, so I figure that will work. Anyway, I really don't like this solution, because it is really a hell to maintain (refactoring etc won't be very easy).
A second solution is to use a FormCollection
. You give this as a parameter of your HttpPost action and you can access then your value through an index. For an example and more information, you can look at this SO post: https://stackoverflow.com/a/5088493/578843 .
The last solution (which I prefer) is creating a ViewModel. I suggest you read this guide ( http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/examining-the-edit-methods-and-edit-view ) on how to do edit pages etc properly.
And one last thing, if you want to submit HTML as content, you will have to either disable the save guarding of ASP.NET or add a Annotation to your method (or class). Please do not generally disable save guards (it will check input for html etc), only disable it with annotations when needed. You can set the ValidateInput
attribute (MSDN link) to false on your action. Example:
[HttpPost]
[ValidateInput(false)]
public ActionResult Description(string textdetails)
{
//Doing something with the text
return RedirectToAction("Create", "Project", new { text = textdetails});
}
Upvotes: 2