0

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 :-)

4
  • I dont think there is an answer to this question. YOu should go over tutorials and understand how things work. Commented Apr 28, 2014 at 14:30
  • I am very new to this, I am trying to port a Classic ASP project, Visual Studio 2013 and MVC 5 is taking some getting used to the new structure. I can see how that Views (.vbhtml) can reference Controllers, so is data bound to the controller and not the View and if so are you aware of any tutorials as I am struggling to find any? Commented Apr 28, 2014 at 14:41
  • Here's a good starting point from Microsoft: asp.net/mvc/tutorials/mvc-music-store Commented Apr 28, 2014 at 15:00
  • Thank you so much, the example is in Visual C# where as I'm using Visual Basic but I get the idea, a massive load off, again thank you :-) Commented Apr 28, 2014 at 15:25

1 Answer 1

6

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.