I'm using TinyMCE editor on admin panel of my site, so i decorate the model properties (target of tinymce) with [AllowHtml] and i use Html.BeginForm() in views. When i submit form with HTML fields all work fine.
But i have a problem if i use the overload Html.BeginForm("action","controller") in the same way, it skips the [AllowHtml] and throw the well-know Request.form exception. I'm forced to use [ValidateInput(false)] on Action-Method to make it work without exception. Do you know why? Thanks in advance for the clarification,
This is the scenario / Project: Asp.net Mvc 4:
Model / Ricetta.cs
..
[Required(ErrorMessage = "Corpo Articolo vuoto")]
[AllowHtml]
public string corpoTesto { get; set; }
..
Controller / RicetteController.cs
..
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(RicettaViewModel modelloRicetta)
{
if (ModelState.IsValid) {
..
View Ricette/Create Called from another Action Method in RicetteController as View("Create", modelObject)
@model WebAPP_MVC4.Areas.Admin.Models.RicettaViewModel
...
@using (Html.BeginForm("Create","Ricette",FormMethod.Post)){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
....
<fieldset>
<legend>Corpo Ricetta ~</legend>
<div class="editor-label">
@Html.LabelFor(p=>p.ricetta.corpoTesto)
</div>
<div class="editor-field">
@Html.TextAreaFor(p=>p.ricetta.corpoTesto, new { @cols = 60, @rows = 20})
@Html.ValidationMessageFor(p=>p.ricetta.corpoTesto)
</div>
</fieldset>
..
The BeginForm () method is an extension method of the HtmlHelper class that writes an opening "<form>" tag and it also calls the "Dispose ()" method that writes a closing "</form>" tag.
We know that when the form is submited, the Post method executes. In the "BeginForm ()" method we did not pass any action and controller name so the default action method means that as the view name action method is called to handle the post of the data request.
The Ajax.BeginForm () method has three arguments, these are actionName, controllerName and AjaxOptions. We are using the AjaxOptions object UpdateTargetId property that has a value of a control id where the response will be shown after the post request.
"BeginForm ()" is an extension method that writes an opening "" tag to the response. "BeginForm ()" is an extension method for both HtmlHelper and AjaxHelper classes.
I made quick test and everything works perfectly there is no difference in behavior between Html.BeginForm() and Html.BeginForm("action","controller"). Maybe the reason of this issue is in the source code which you didn't show us.
Below my code(works):
VieModel:
public class PostViewModel
{
[AllowHtml]
[Required]
public string Content { get; set; }
}
Controller:
public ActionResult Index()
{
return View("Create", new PostViewModel());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(PostViewModel model)
{
if (ModelState.IsValid)
{
return Index();
}
return View(model);
}
View:
@model SendHTmlTpControler.Models.PostViewModel
<html>
<head>
<script src="~/Scripts/tinymce/tiny_mce.js"></script>
<script type="text/javascript">
tinymce.init({
selector: "textarea",
toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
});
</script>
</head>
<body>
<h2>Create</h2>
@using (Html.BeginForm("Create", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.Content)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Content, new { @cols = 60, @rows = 20 })
@Html.ValidationMessageFor(model => model.Content)
</div>
<p>
<input type="submit" value="Save" />
</p>
}
</body>
</html>
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