首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASP.NET -MVC5SQL Server数据库导航

ASP.NET -MVC5SQL Server数据库导航
EN

Stack Overflow用户
提问于 2014-04-28 22:26:49
回答 1查看 421关注 0票数 0

有人能解释一下如何将数据从SQL Server数据库插入到现有的ASP.NET MVC5项目中吗?

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

代码语言:javascript
复制
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>

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

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

EN

回答 1

Stack Overflow用户

发布于 2017-03-31 15:05:20

下面是解决你的问题的方法。

你的控制器代码

代码语言:javascript
复制
        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视图文件中。

代码语言:javascript
复制
<div>
    @if (ViewBag.DynamicActions != null)
    {
      @Html.Raw(ViewBag.DynamicActions)
    }
</div>
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23343738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档