【发布时间】:2016-03-23 07:13:02
【问题描述】:
我刚刚设置了一个本地 SQL 数据库,我正在 MVC 网站中使用 EF,我想知道是否有人可以对在视图中使用模型有所了解。我似乎遇到了困难,因为我需要通过使用单独的查询来查询数据库来使用同一模型的多个版本。 (研究似乎指向了 LINQ-to-SQL)
无论如何 这是我刚刚的控制器类
public ActionResult Clubs(int comp)
{
var dbContext = new DataDBEntities();
switch (comp)
{
case 1:
var query = from clubs in dbContext .Clubs where clubs.League_Table == 1 select clubs;
var PremList = query.ToList();
return View(PremList.ToList());
case 2:
var query1 = from clubs in dbContext .Clubs where clubs.League_Table == 2 select clubs;
var Div1List = query1.ToList();
return View(Div1List.ToList());
case 3:
var query2 = from clubs in dbContext .Clubs where clubs.League_Table == 3 select clubs;
var Div2AList = query2.ToList();
return View(Div2AList.ToList());
case 4:
var Div2BContext = new DataDBEntities();
var query3 = from clubs in dbContext .Clubs where clubs.League_Table == 4 select clubs;
var Div2BList = query3.ToList();
return View(Div2BList.ToList());
case 5:
var query4 = from clubs in dbContext .Clubs where clubs.League_Table == 5 select clubs;
var Div2CList = query4.ToList();
return View(Div2CList.ToList());
case 6:
var query5 = from clubs in dbContext .Clubs where clubs.League_Table == 6 select clubs;
var Div2DList = query5.ToList();
return View(Div2DList.ToList());
default:
break;
}
return View(db.Clubs.ToList());
}
在视图中我使用@model IEnumerable
@model IEnumerable<SundayCentralAFL.Models.Club>
@{
ViewBag.title = "Clubs Contacts";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<section id="bodyWrapper">
<section id="leftBody">
<section id="leftAds">
<section class="ads">
<img src="../Pictures/Logo.png" alt="Sunday Central AFL" />
</section>
<section class="ads">
<img src="../Pictures/Logo.png" alt="Sunday Central AFL" />
</section>
</section>
<section id="clubList">
<div id="accordion">
<div>
<h2>Premier Division</h2>
<table class="contactTable">
<tr>
<th>Club Name</th>
<th>Manager Name</th>
<th>Contact Details</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Club_Name)</td>
<td>@Html.DisplayFor(modelItem => item.Manager)</td>
<td>@Html.DisplayFor(modelItem => item.Contact_Number)</td>
</tr>
}
</table>
</div>
<div>
<h2>Division 1</h2>
<table class="contactTable">
<tr>
<td>Club Name</td>
<td>Manager Name</td>
<td>Contact Details</td>
</tr>
</table>
</div>
<div>
<h2>Division 2A</h2>
<table class="contactTable">
<tr>
<td>Club Name</td>
<td>Manager Name</td>
<td>Contact Details</td>
</tr>
</table>
</div>
<div>
<h2>Division 2B</h2>
<table class="contactTable">
<tr>
<td>Club Name</td>
<td>Manager Name</td>
<td>Contact Details</td>
</tr>
</table>
</div>
<div>
<h2>Division 2C</h2>
<table class="contactTable">
<tr>
<td>Club Name</td>
<td>Manager Name</td>
<td>Contact Details</td>
</tr>
</table>
</div>
<div>
<h2>Division 2D</h2>
<table class="contactTable">
<tr>
<td>Club Name</td>
<td>Manager Name</td>
<td>Contact Details</td>
</tr>
</table>
</div>
</div>
</section>
</section>
<section id="rightBody">
<section class="ads">
<a href="https://www.facebook.com/sundaycentralmedia" alt="Sunday Central AFL" title="Sunday Central AFL" target="_blank"><img src="../Pictures/Logo.png" alt="Sunday Central AFL" /></a>
</section>
<section class="ads">
<img src="../Pictures/Logo.png" alt="Sunday Central AFL" />
</section>
<section class="ads">
<a href="https://www.facebook.com/sundaycentralmedia" alt="Sunday Central AFL" title="Sunday Central AFL" target="_blank"><img src="../Pictures/Logo.png" alt="Sunday Central AFL" /></a>
</section>
<section class="ads">
<img src="../Pictures/Logo.png" alt="Sunday Central AFL" />
</section>
</section>
</section>
现在,对于每个单独的排名表,我希望使用 foreach 循环或仅使用 for 循环来根据各个查询填充表。如果在 URL 链接中没有分配表/错误的表,则默认需要运行所有 6 个查询并生成视图所需的数据。
当只使用一个查询并将列表传递到视图时,这似乎确实有效,但我需要传递所有受人尊敬的分区俱乐部列表,然后在另一端使用。
我已经看到了使用 viewModels 的可能性,但我不确定这种方法。
另一种方法是使用部分视图,但这不会遇到与主视图相同的问题,因为我只能通过一个包含所有俱乐部的整体模型,而不是所需的单个俱乐部列表。
任何需要的进一步信息可以帮助询问
谢谢
【问题讨论】:
标签: c# asp.net-mvc entity-framework linq