Reputation: 2483
Could somebody please explain how to insert data from a SQL Server database into an existing ASP.NET MVC 5 project?
I have a file Shared/_layout.vbhtml
which has the following lines:
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
However I would like to replace this with the actual data from the Navigation table. I have added a new Data Connection to SQL Server within the ASP.NET MVC 5 project, but I am now unsure as how to bind the connection this page.
Any examples or advice would be much appreciated :-)
Upvotes: 0
Views: 481
Reputation: 2112
Below is the way to solve out your problem.
Your controller code
public ActionResult Index()
{
StringBuilder sb = new StringBuilder();
// Simply fetch from the database and set the action name and controller name with your own logic like using of loop or any.
// Also, You have to set style and css what you want exactly as per your theme.
sb.AppendFormat("First Link", Url.Action("ActionName", "ControllerName"));
sb.AppendFormat("Second Link", Url.Action("ActionName", "ControllerName"));
sb.AppendFormat("Third Link", Url.Action("ActionName", "ControllerName"));
// Set ViewBag property with string builder object
ViewBag.DynamicActions = sb.ToString();
return View();
}
Place the below code in _layout view file.
<div>
@if (ViewBag.DynamicActions != null)
{
@Html.Raw(ViewBag.DynamicActions)
}
</div>
Upvotes: 6