[英]Using Multiple versions of the same entity framework class using @model
i have a local SQL db set up just now and i was working on using EF in an MVC website and I'm wondering if anyone could possible shed some light on using models in the View.我刚刚设置了一个本地 SQL 数据库,我正在 MVC 网站中使用 EF,我想知道是否有人可以对在视图中使用模型有所了解。 I seem to have hit a stag in that I need to use multiple version of the same model by querying the database with separate queries.
我似乎遇到了麻烦,因为我需要通过使用单独的查询查询数据库来使用同一模型的多个版本。 (The research seemed to point to LINQ-to-SQL for this)
(该研究似乎指向 LINQ-to-SQL)
Anyway This is the controller class that i have just now无论如何,这是我刚刚拥有的控制器类
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());
}
and in the View i am using @model IEnumerable在视图中我使用@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>
Now, for each of the individual league tables i am hoping to use a foreach loop or just a for loop to populate the tables based on the individual queries.现在,对于每个单独的联赛表,我希望使用 foreach 循环或仅使用 for 循环来根据单个查询填充表。 Default will be required to run all 6 queries ad produce the required data for the view if no table/wrong table is assigned in the URL link.
如果在 URL 链接中没有分配表/错误表,则默认将需要运行所有 6 个查询广告生成视图所需的数据。
This does appear to be working when using just one query and passing the list to the view but i need all the respected division club lists to be passed and then used at the other end.当仅使用一个查询并将列表传递给视图时,这似乎确实有效,但我需要传递所有受人尊敬的分区俱乐部列表,然后在另一端使用。
I have seen the possibility in using viewModels but the I am unsure of the approach.我已经看到了使用 viewModels 的可能性,但我不确定这种方法。
Another would be to use partialViews, but would that not run into the same issues as the main view in that I could only pass one overall model with all the clubs and not the individual list of club required.另一种方法是使用部分视图,但这不会遇到与主视图相同的问题,因为我只能传递一个包含所有俱乐部的整体模型,而不是所需的单个俱乐部列表。
Any further information needed that could help just ask需要的任何进一步信息可以帮助只是询问
Thanks谢谢
You Clubs
action can be reduced as I show as follow: You
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());
}
}
Now I saw you need all the leagues.现在我看到你需要所有的联赛。 In that case you need to do a group by:
在这种情况下,您需要通过以下方式进行分组:
var query= context.Clubs.GroupBy(c=>c.League_Table);
But you are going to need to change the type of your model to IEnumerable<IGrouping<int, Club>>
, and use two cycles in your view, the first one to iterate per each group, and the second one to iterate per each clubs that belong to the current group.但是您需要将模型的类型更改为
IEnumerable<IGrouping<int, Club>>
,并在您的视图中使用两个循环,第一个循环每个组迭代,第二个循环每个俱乐部迭代属于当前组。 Now but there is a problem with this solution, you don't have the league's name with this query.现在但是这个解决方案有一个问题,你没有这个查询的联盟名称。 I guess
League_Table
is a FK property in your Club
entity.我猜
League_Table
是您Club
实体中的 FK 财产。 If that is the case you should have a League
navigation property, so you could group by League name instead by Id:如果是这种情况,您应该有一个
League
导航属性,因此您可以按 League 名称而不是按 Id 分组:
var query= context.Clubs.GroupBy(c=>c.League.Name);
So, your model type would be in this case IEnumerable<IGrouping<string, Club>>
, and you could do something like this inside your accordion:因此,在这种情况下,您的模型类型将是
IEnumerable<IGrouping<string, Club>>
,您可以在手风琴中执行以下操作:
@foreach (var group in Model)
{
<div>
<h2>group.Key</h2>
<table class="contactTable">
<tr>
<th>Club Name</th>
<th>Manager Name</th>
<th>Contact Details</th>
</tr>
@foreach (var item in group)
{
<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>
}
You're not setting the ViewData.Model
parameter, so the @model diretive doesn't have any data to work with.您没有设置
ViewData.Model
参数,因此 @model 指令没有任何数据可以使用。 Also, we can improve your code a little bit:此外,我们可以稍微改进您的代码:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.