I have added TinyMCE
editor to add description in HTML format but when I write something in HTML and click Add Product
It gives me this error:
A potentially dangerous Request.Form value was detected from the client (Description="
I've tried:
AllowHtml
<= not working[HttpPost, ValidateInput(true, Exclude = "Description")]
and get this error
System.Web.Mvc.ValidateInputAttribute' does not contain a definition for Exclude
<httpRuntime requestValidationMode="2.0">
in web.config and getting this error
HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related configuration data for the page is invalid.
Product.cs
public partial class Product {
public int productID {get; set;}
[Required]
public int Name {get; set;}
[AllowHtml]
public string Description {get; set;}
public string ImagePath {get;set}
}
Add Product View
@using (Html.BeginForm("AddProduct", "Store", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Create a new product.</h4>
<hr />
@Html.ValidationSummary(true)
@ViewBag.SizeMsg
<div class="form-group">
@Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label"})
<div class="col-md-10">
@Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
@Html.ValidationMessageFor(m=>m.Name)
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Description, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextAreaFor(m => m.Description, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.ImagePath, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
<input type="file" name="file" id="file" style="width: 100%;" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Add Product" />
</div>
</div>
}
Controller
[HttpPost]
public ActionResult AddProduct(HttpPostedFileBase file)
{
if (file != null)
{
var allowedExtensions = new[] { ".jpg", ".png", ".jpeg", ".gif", ".JPG", ".PNG", ".JPEG" };
if (allowedExtensions.Contains(extension))
{
string ImagePath = System.IO.Path.GetFileName(file.FileName);
string physicalPath = Server.MapPath("~/ProductImages/" + ImagePath);
file.SaveAs(physicalPath);
Product newRecord = new Product();
newRecord.Name = Request.Form["Name"];
newRecord.Description = Request.Form["Description"];
newRecord.ImagePath = ImagePath;
db.Products.Add(newRecord);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
else
{
ViewBag.SizeMsg = "File not supported.";
return View();
}
}
return View();
}