【发布时间】:2016-08-13 06:05:40
【问题描述】:
我有一个 MVC 应用程序,我试图从 ADO.Net 实体数据模型中获取 SQL,以显示在我的 index.cshtml 中
这个页面后面我的家庭控制器是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using TemplateBootstrap.Models;
namespace TemplateBootstrap.Controllers
{
public class HomeController : Controller
{
private DBEntities db = new DBEntities();
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
在我的index.cshtml 中,目前我在我的导航标题和内容中硬编码了很多 HTML,这些内容存储在我的数据库的 5 个表中,例如主导航 1/2、侧导航 1/2 和内容。
下面是用于 MainNavLevel1 的 SQL 语句。
SELECT [MNavID], [DisplayLabel],[Priority] FROM [MainNavLevel1] ORDER BY [Priority]"
这是我使用一个没有 SQL 代码的表的示例。
controller: namespace TemplateBootstrap.Controllers
{
public class NavigationController : Controller
{
private AskHoltsEntities db = new AskHoltsEntities();
public ActionResult Index()
{
return View(db.AH_Corp_MainNavLevel1.ToList());
}
}
}
使用此视图:
@model IEnumerable<TemplateBootstrap.Models.AH_Corp_MainNavLevel1>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>@Html.DisplayNameFor(model => model.DisplayLabel)</th>
<th>@Html.DisplayNameFor(model => model.URL)</th>
<th>@Html.DisplayNameFor(model => model.Priority)</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td> @Html.DisplayFor(modelItem => item.DisplayLabel) </td>
<td> @Html.DisplayFor(modelItem => item.URL)</td>
<td> @Html.DisplayFor(modelItem => item.Priority)</td>
</tr>
}
</table>
我需要帮助来更改代码或添加(类)以放置我的 SQL 语句以从多个表中检索数据。
【问题讨论】:
-
有什么想法吗?
-
你想做什么?您需要指定要在页面上显示的内容,以便我们了解目标。
-
有什么问题?
-
我应该把 SQL 查询放在哪里来帮助做到这一点?
-
您使用的是 ORM 还是使用 ado.net?
标签: c# asp.net sql-server asp.net-mvc asp.net-mvc-4