【问题标题】:ASP.NET - MVC 5 SQL Server Database NavigationASP.NET - MVC 5 SQL Server 数据库导航
【发布时间】:2014-04-28 14:26:48
【问题描述】:

谁能解释一下如何将 SQL Server 数据库中的数据插入到现有的 ASP.NET MVC 5 项目中?

我有一个文件Shared/_layout.vbhtml,其中包含以下几行:

<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>

但是我想用导航表中的实际数据替换它。我在 ASP.NET MVC 5 项目中添加了到 SQL Server 的新数据连接,但我现在不确定如何绑定此页面的连接。

任何示例或建议将不胜感激:-)

【问题讨论】:

  • 我认为这个问题没有答案。您应该阅读教程并了解其工作原理。
  • 我对此很陌生,我正在尝试移植一个经典的 ASP 项目,Visual Studio 2013 和 MVC 5 需要一些时间来适应新的结构。我可以看到视图(.vbhtml)如何引用控制器,数据绑定到控制器而不是视图,如果是这样,您是否知道任何教程,因为我正在努力寻找任何教程?
  • 这是 Microsoft 的一个很好的起点:asp.net/mvc/tutorials/mvc-music-store
  • 非常感谢,示例是在 Visual C# 中,因为我使用的是 Visual Basic,但我明白了,减轻了很多负担,再次感谢 :-)

标签: sql-server asp.net-mvc navigation


【解决方案1】:

以下是解决问题的方法。

您的控制器代码

        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();

        }

将以下代码放入_layout视图文件中。

<div>
    @if (ViewBag.DynamicActions != null)
    {
      @Html.Raw(ViewBag.DynamicActions)
    }
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-29
    • 2021-08-14
    • 2018-10-01
    • 2017-02-07
    • 1970-01-01
    • 2011-01-22
    • 2018-07-07
    • 1970-01-01
    相关资源
    最近更新 更多