有人能解释一下如何将数据从SQL Server数据库插入到现有的ASP.NET MVC5项目中吗?
我有一个文件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 MVC5项目中添加了一个到SQL Server的新数据连接,但我现在不确定如何绑定此页面的连接。
任何例子或建议都将不胜感激:-)
发布于 2017-03-31 15:05:20
下面是解决你的问题的方法。
你的控制器代码
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>https://stackoverflow.com/questions/23343738
复制相似问题