我对MVC很陌生,我试图从8个不同的表中提取数据(每个表中有1条记录,但每个表中每条记录有几个字段)
我阅读了如何使用2个表并加入,但是当我尝试加入两个以上的表时,我会收到错误。
每个表在每条记录中都有一个ID字段,这就是我需要查询的内容。
更新:
这是我的ViewModel:
public class StoreInfoViewModel
{
public List<AWS_Ticket> tickets { get; set; }
public List<AWS_TNF_StepOne> tnf { get; set; }
public List<AWS_TNF_StepTwo> tnf2 { get; set; }
public List<AWS_DBU> dbu { get; set; }
public List<GetStoreInfoResult> storeInfo { get; set; }
}
这是我的控制器:
public class StoreInfoController : Controller
{
PortalDataContext db = new PortalDataContext();
//
// GET: /StoreInfo/
public ActionResult Index(int id)
{
var tickets = db.AWS_Tickets.Where(x => x.Store_Number == id);
var tnf = db.AWS_TNF_StepOnes.Where(x => x.Store_Number == id);
var tnf2 = db.AWS_TNF_StepTwos.Where(x => x.Store_Number == id);
var dbu = db.AWS_DBUs.Where(x => x.StoreNumber == id);
var storeInfo = db.GetStoreInfo(id);
StoreInfoViewModel model = new StoreInfoViewModel();
model.tickets = tickets.ToList();
model.tnf = tnf.ToList();
model.tnf2 = tnf2.ToList();
model.dbu = dbu.ToList();
model.storeInfo = storeInfo.ToList();
return View(model);
}
这是我的观点:
@model IEnumerable<Team_Portal_v3.Models.StoreInfoViewModel>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div>
<table>
@foreach (var item in Model)
{
<tr>
<td>
Store Number: @Html.DisplayFor(modelItem => item.storeInfo[0].StoreNum)
</td>
<td> </td>
<td>24 Hour Store? @Html.DisplayFor(modelItem => item.storeInfo[0].Is24Hour)</td>
</tr>
<tr>
<td>
City / State: @Html.DisplayFor(modelItem => item.storeInfo[0].City) , @Html.DisplayFor(modelItem => item.storeInfo[0].State)
</td>
<td>
Store Phone: @Html.DisplayFor(modelItem => item.storeInfo[0].StorePhone)
</td>
<td>
DBU Installed? @Html.DisplayFor(modelItem => item.storeInfo[0].DBUInstalled)
</td>
</tr>
<tr>
<td>
Owner: @Html.DisplayFor(modelItem => item.storeInfo[0].Owner)
</td>
<td>
Owner Phone: @Html.DisplayFor(modelItem => item.storeInfo[0].OwnerPhone)
</td>
<td>
Store Status: @Html.DisplayFor(modelItem => item.storeInfo[0].StoreStatus)
</td>
</tr>
<tr>
<td>
Division: @Html.DisplayFor(modelItem => item.storeInfo[0].Division)
</td>
<td>
Region: @Html.DisplayFor(modelItem => item.storeInfo[0].Region)
</td>
<td>
Expected Opening Date: @Html.DisplayFor(modelItem => item.storeInfo[0].OpeningDate)
</td>
</tr>
}
</table>
</div>
我没有收到关于init运行应用程序的任何错误,当我尝试使用和数字ID转到索引时,我收到以下错误:
传递到字典中的模型项是类型的 &#39; Team_Portal_v3.Models.StoreInfoViewModel&#39;,但是这本词典 需要类型的模型项 &#39; System.Collections.Generic.IEnumerable`1 [Team_Portal_v3.Models.StoreInfoViewModel]&#39;
我哪里出错了?
答案 0 :(得分:0)
我不想不受欢迎,但是当您跨越3个联接时,最好使用存储过程。 Here is article on how to do it.
答案 1 :(得分:0)
这里的错误信息非常明显。您传递了一个StoreInfoViewModel
,但您的观点期待IEnumberable<StoreInfoViewModel>
。
您的观点需要更新为只需要一个StoreInfoViewModel
@model Team_Portal_v3.Models.StoreInfoViewModel
或者您需要从Controller / Action
中实际传入StoreInfoViewModel
列表