我刚刚建立了一个本地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());
}
并且在View中我正在使用@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的可能性,但我不确定这种方法。
另一种方法是使用partialViews,但不会遇到与主视图相同的问题,因为我只能传递一个包含所有俱乐部的整体模型,而不是单独的俱乐部列表。
所需的任何其他信息可以帮助您提问
由于
答案 0 :(得分:1)
您可以减少from subprocess import Popen, PIPE, STDOUT
cmd = 'bash -i -c "alias"'
event = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
output = event.communicate()[0]
print(output)
行动,如下所示:
Clubs
现在我看到你需要所有的联赛。在这种情况下,您需要通过以下方式进行分组:
public ActionResult Clubs(int comp)
{
using(var context=new DataDBEntities())
{
var query= context.Clubs;
if(comp!=0)//default value
{
query=query.Where(c=>c.League_Table ==comp);
}
return View(query.ToList());
}
}
但是您需要将模型的类型更改为var query= context.Clubs.GroupBy(c=>c.League_Table);
,并在视图中使用两个循环,第一个循环用于每个组,第二个循环用于每个俱乐部属于当前组。现在但是这个解决方案存在问题,你没有这个查询的联盟名称。我猜IEnumerable<IGrouping<int, Club>>
是League_Table
实体中的FK属性。如果是这种情况,您应该拥有Club
导航属性,因此您可以按联盟名称而不是Id进行分组:
League
所以,你的模型类型就是var query= context.Clubs.GroupBy(c=>c.League.Name);
,你可以在手风琴里做类似的事情:
IEnumerable<IGrouping<string, Club>>
答案 1 :(得分:0)
您未设置ViewData.Model
参数,因此@model diretive没有任何数据可供使用。此外,我们可以稍微改进您的代码:
public ActionResult Clubs(int comp)
{
var dbContext = new DataDBEntities();
var query = from clubs in dbContext.Clubs where clubs.League_Table == comp select clubs;
var data = query.ToList();
if (data.Count == 0) ViewData.Model = dbContext.Clubs.ToList();
else ViewData.Model = data;
return View();
}