【问题标题】:Populate View with 2 Lists (Group by LINQ queries)使用 2 个列表填充视图(按 LINQ 查询分组)
【发布时间】:2017-03-02 05:14:21
【问题描述】:

我是 ASP.net MVC 的新手,我已经查看了其他问题并尝试了多种方法来使用 ViewModel 填充具有 2 个 IEnumerables 的视图。似乎在控制器中使用 groupby 方法并将它们传递给 View 会产生一些问题。

总体问题是视图不接受 ListCatViewModel。它给出了错误:IEnumerable 不包含“类别”的定义,并且没有扩展方法“类别”接受“IEnumerable”类型的第一个参数

在我在 Stackoverflow 和其他来源看到的多个问题中,我们应该将两个列表与一个变量结合起来,然后将该变量传递给视图。之后我们应该引用 ViewModel。当我引用 ViewModel 时,我无法访问每个 IEnumerable(NotesList 和 RelList)中的属性。我正在寻找有关如何更改视图中的控制器和/或引用以接受 2 个单独的集合以最终创建两个表的方向。

代码总结:CatViewModel 和 RelViewModel 拥有不同的属性。 ListCatViewModel 包含 IEnumerable 集合。

     public class CatViewModel
    {
        public string Category { get; set; }

        public decimal CountOfLoans { get; set; }

        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
        public decimal TotalVolume { get; set; }

        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
        public decimal UnfundedCommitment { get; set; }     
    }

 public class RelViewModel
    {
      public string RelationshipName { get; set; }

        public decimal CountOfLoans { get; set; }

        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
        public decimal TotalVolume { get; set; }

        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
        public decimal UnfundedCommitment { get; set; }
    }



public class ListCatViewModel
    {    
        public IEnumerable<CatViewModel> NotesList { get; set; }
        public IEnumerable<RelViewModel> RelList { get; set; }

    }

控制器:

public ActionResult Index()
        {
            ApplicationDbContext db = new ApplicationDbContext();

            ListCatViewModel LCVM = new ListCatViewModel();

            LCVM.NotesList = GetCatList();
            LCVM.RelList = GetRelList();

            return View(LCVM);
        }

        public IEnumerable<CatViewModel> GetCatList()  //Category group by query
        {
            ApplicationDbContext db = new ApplicationDbContext();

            IEnumerable<CatViewModel> CatVM = db.LoanPortfolio.GroupBy(i => i.Category)


                .Select(g => new CatViewModel
                {
                    Category = g.Key,
                    CountOfLoans = g.Count(),
                    TotalVolume = g.Sum(i => i.Volume),
                    UnfundedCommitment = g.Sum(i => i.Unfunded),
                    //Average = g.Average(i => i.Volume)
                })
             .OrderBy(c => c.Category)

             .AsEnumerable();

            return CatVM;

        }

        public IEnumerable<RelViewModel> GetRelList()


        {

            ApplicationDbContext db = new ApplicationDbContext();
            IEnumerable<RelViewModel> RelVM = db.LoanPortfolio.GroupBy(i => i.RelationshipName)




                 .Select(g => new RelViewModel
                 {
                     RelationshipName = g.Key,
                     CountOfLoans = g.Count(),
                     TotalVolume = g.Sum(i => i.Volume),
                     UnfundedCommitment = g.Sum(i => i.Unfunded),
                     //Average = g.Average(i => i.Volume)
                 })
              .OrderBy(c => c.RelationshipName)

              .AsEnumerable();

            return RelVM;
        }

查看:

查看:

@model IEnumerable<Risk.ViewModel.ListCatViewModel>

@{
    ViewBag.Title = "GroupByLoanPAllowanceB";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Category</h2>




<table class="table">
    <tr>


        <th>
            @Html.DisplayNameFor(model => model.NotesList.Category)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NotesList.CountOfLoans)
        </th>

        <th>
            @Html.DisplayNameFor(model => model.NotesList.TotalVolume)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NotesList.UnfundedCommitment)
        </th>

    </tr>

    @foreach (var item in Model)
    {
        <tr>


            <td>
                @Html.DisplayFor(modelItem => item.NotesList.Category)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CountOfLoans)
            </td>

            <td>
                @Html.DisplayFor(modelItem => item.TotalVolume)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UnfundedCommitment)
            </td>

        </tr>
    }

</table>

【问题讨论】:

  • @foreach (var item in Model.NotesList)@foreach (var item in Model.RelList)
  • 或者创建一个编辑器或显示模板,分别使用EditorForDisplayFor
  • 如何在每个 foreach 代码块上方添加列名的单次迭代?我需要能够在每个列表中引用它们。

标签: c# asp.net-mvc linq


【解决方案1】:

你的视图不是IEnumerable&lt;Risk.ViewModel.ListCatViewModel&gt;,现在是ListCatViewModel

试试:

@model Risk.ViewModel.ListCatViewModel

@{
    ViewBag.Title = "GroupByLoanPAllowanceB";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

当遍历列表时,您需要从Model 访问它

@foreach (var item in Model.NotesList)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Category)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CountOfLoans)
        </td>

        <td>
            @Html.DisplayFor(modelItem => item.TotalVolume)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UnfundedCommitment)
        </td>

    </tr>
}

【讨论】:

  • 感谢您的 cmets。他们很有帮助。这适用于 Displayfor 下的数据。如何更改 DisplayNameFor 以获取列标题?
  • 这是我输入的代码,它为每行数据提供了一个列标题。我该如何改变? @foreach(Model.NotesList 中的var item) { @Html.DisplayNameFor(modelItem => item.Category)
【解决方案2】:

假设您只是想显示数据,在Views -&gt; Shared -&gt; DisplayTemplates 文件夹中创建一些模板(您可能需要创建它),然后使用DisplayFor。这是您的 CatViewModel 的示例:

CatViewModel.cshtml

@model Your.Fully.Qualified.Namespace.CatViewModel

<tr>
    <td>
        @Model.Category
    </td>
    <td>
        @Model.CountOfLoans
    </td>

    <td>
        @Model.TotalVolume
    </td>
    <td>
        @Model.UnfundedCommitment
    </td>
</tr>

父视图:

@model Risk.ViewModel.ListCatViewModel

@{
    ViewBag.Title = "GroupByLoanPAllowanceB";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Category</h2>

<table class="table">
    <tr>
        <th>
            Category
        </th>
        <th>
            Count Of Loans
        </th>

        <th>
            Total Volume
        </th>
        <th>
            Unfunded Commitment
        </th>
    </tr>

    @Html.DisplayFor(m => m.NotesList)
</table>

当您调用EditorForDisplayFor 时,Razor 引擎非常智能,可以查找与模型(在上述文件夹中)同名的局部视图。因此,如果属性的类型为Foo,并且您有一个名为Foo.cshtml 的视图,那么它将用于该属性。如果您提供一个集合,它还将遍历该集合并为每个项目生成标记。

还有an overload that allows you to specify the template to use,如果你不想遵循我上面提到的约定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多