How to allow HTML content as Input in MVC
As we know MVC supports built in functionality to protect the Cross site scripting and SQL injection as well. But sometimes as per our requirements we need some HTML content as a Input like in blogging application.
So to consider the HTML content as Input we have different way to handle in MVC5 application .
CASE - 1 :
In this case we will allow to a post type action method to accept the HTML content like
[HttpPost][ValidateInput(False)]
public ActionResult Create([Bind(Include = "Id,Title,Description,Image,IsActive,PostedBy,PostedOn")] Blog blog) {
if (ModelState.IsValid) {
db.Blogs.Add(blog);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(blog);
}
[ValidateInput(false)] attribute disables request validation on complete model or view model, but we want to allow html on only few properties of model or view model, for example in Blog model class contains three properties Title, Description .
CASE - 2 :
In this case we will allow only some specific property like
public partial class Blog
{
public int Id { get; set; }
public string Title { get; set; }
[AllowHtml]
public string Description { get; set; }
public string Image { get; set; }
public Nullable<bool> IsActive { get; set; }
public string PostedBy { get; set; }
public Nullable<System.DateTime> PostedOn { get; set; }
}
So here we use [AllowHtml] attribute to specific property that means only the descriptions input will allow the HTML content not other input filed like Title etc.
So here we use [AllowHtml] attribute to specific property that means only the descriptions input will allow the HTML content not other input filed like Title etc.
So you Implement the concept as per your application need. Keep enjoying .
Please like this Tips if it really helps you.
Comments
Post a Comment