Html.BeginForm () works fine, Html.BeginForm ("action", "controller") ignores [AllowHtmlAttribute]

I use the TinyMCE editor in the admin panel of my site, so I decorate the model properties (the tinymce target) with [AllowHtml] , and I use Html.BeginForm () in the views. When I submit a form with HTML fields, everything works fine.

But I have a problem, if I use the Html.BeginForm overload ("action", "controller") in the same way, it skips [AllowHtml] and throw the well-known Request.form exception. I have to use [ValidateInput (false)] in the Action-Method to make it work without exception. You know why? Thanks in advance for clarifying

This is the script / 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 the RicetteController as a 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> .. 
+4
source share
1 answer

I did a quick test, and everything works fine, there is no difference in behavior between Html.BeginForm () and Html.BeginForm ("action", "controller"). Perhaps the reason for this question is in the source code that you did not show us.

Below is 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> 
+5
source

Source: https://habr.com/ru/post/1485140/


All Articles