To show HTML data from a database in an ASP.NET MVC front-end view, you can follow these steps:
Step 1: Retrieve the HTML data from the database
You can retrieve the HTML data from the database using Entity Framework or any other ORM tool or data access method you are using. Here’s an example using Entity Framework:
public ActionResult ShowHtmlData(int id)
{
var model = db.HtmlData.Find(id);
return View(model);
}
In this code, we are retrieving the HTML data from the database by finding the record with the specified ID in the HtmlData table.
Step 2: Create a view to display the HTML data
Create a view to display the HTML data. In the view, you can use the Html.Raw helper method to display the HTML content without escaping any characters.
@model YourNamespace.Models.HtmlData
@Model.Title
@Html.Raw(Model.HtmlContent)
In this code, we are using the Html.Raw helper method to display the HTML content without escaping any characters.
Step 3: Render the view
Render the view from the controller action by returning it from the action method.
public ActionResult ShowHtmlData(int id)
{
var model = db.HtmlData.Find(id);
return View(model);
}