【问题标题】:Using Multiple versions of the same entity framework class using @model使用@model 使用同一实体框架类的多个版本
【发布时间】: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


    【解决方案1】:

    您的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&lt;IGrouping&lt;int, Club&gt;&gt;,并在您的视图中使用两个循环,第一个循环针对每个组进行迭代,第二个循环针对属于的每个俱乐部进行迭代当前组。现在但是这个解决方案有一个问题,这个查询没有联赛的名字。我猜 League_Table 是您的 Club 实体中的 FK 属性。如果是这种情况,您应该有一个 League 导航属性,因此您可以按联赛名称而不是 Id 分组:

    var query= context.Clubs.GroupBy(c=>c.League.Name);
    

    因此,在这种情况下,您的模型类型将是 IEnumerable&lt;IGrouping&lt;string, Club&gt;&gt;,您可以在手风琴内执行类似的操作:

    @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>
    }
    

    【讨论】:

    • 非常感谢@octavioccl 这似乎更好地解释了事情。我知道控制器可以改进,但不知道如何最好地解释这个问题。出于这个原因,League_Table 仅用作联赛指标再次感谢您的回答,并在稍作更改后帮助其工作得很好;)我不得不删除 query=query.where(c=>c.League_Table ==comp);在我输入 var query= context.Clubs.GroupBy(c=>c.League_Table);但现在可以使用
    【解决方案2】:

    您没有设置ViewData.Model 参数,因此@model 指令没有任何数据可以使用。此外,我们可以稍微改进您的代码:

    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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      相关资源
      最近更新 更多