【问题标题】:Display viewmodel data from controller in view in ASP.NET Core在 ASP.NET Core 的视图中显示来自控制器的视图模型数据
【发布时间】:2018-10-26 18:45:27
【问题描述】:

我想在 Razor 视图中显示查询结果。

我通过控制器获得了所需的输出,但我需要帮助才能在视图中显示。

班级Fuel:

public class Fuel
{
    public int FuelId { get; set; }
    public int FMonth{ get; set; }
    public int Year { get; set; }
}

班级Location:

public class Location
{
    public int LocationId { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
}

现在我使用视图模型类 FuelLocation 加入两个表:

 public class FuelLocation
 {
    public Fuel Fuels { get; set; }
    public Location Locations { get; set; }       
 }

这是控制器中的操作方法:

public ActionResult EmpDetails()
{
    var res = (from loc in _context.Locations
               join Fl in _context.Fuels on loc.Name equals Fl.Locationn
               select new FuelLocation
                       {
                           Fuels = Fl,
                           Locations = loc
                       }).GroupBy(c => c.Fuels.Locationn)
              .Select(g => new
              {
                  Location = g.Key,
                  Jan = g.Where(c => c.Fuels.FMonth == 1).Sum(c => c.Fuels.Sale),
                  Feb = g.Where(c => c.Fuels.FMonth == 2).Sum(c => c.Fuels.Sale),
                  March = g.Where(c => c.Fuels.FMonth == 3).Sum(c => c.Fuels.Sale)
              }).ToList();
}

这个查询的结果是:

Output

我想这样展示它

Name    | Jan       | Feb       | Mar   | Total
1       | 100       | 350       |  250  | 700
2       | 200       | 220       | 2150  | 2170

我的剃刀视图:

 @model IList<COSAuthNew.Services.Generic.FuelLocation>
        <h2>Index</h2>
        <table class="table">
                <tr>
                    <th>LocationName</th>
                    <th>Jan</th>
                    <th>Feb</th>
                    <th>March</th>
                     <th>Total</th>
                </tr>
                @foreach (var item in Model)
                {
                    //Plz enter your solution 
                }

但我没有得到我想要的正确结果。

谢谢

【问题讨论】:

    标签: c# entity-framework asp.net-core


    【解决方案1】:

    要显示项目值,只需在 td-tag 中的值之前添加@。然后您可以使用@(val1 + val2..) 对最后一列中的值求和

    @model IList<COSAuthNew.Services.Generic.FuelLocation>
            <h2>Index</h2>
            <table class="table">
                    <tr>
                        <th>LocationName</th>
                        <th>Jan</th>
                        <th>Feb</th>
                        <th>March</th>
                        <th>Total</th>
                    </tr>
                    @foreach (var item in Model)
                    {
                        <td>@item.Location</td>
                        <td>@item.Jan</td>
                        <td>@item.Feb</td>
                        <td>@item.March</td>
                        <td>@(item.Jan + item.Feb + item.March)</td>
                    }
    

    【讨论】:

    • 亲爱的,请关注燃料类,“一月”、“二月”、“三月”没有属性
    • 然后我们如何在视图中显示 @item.Location @item.Jan @item.Feb @item.March
    【解决方案2】:

    对于Razor View,您需要返回一个包含LocationJan 属性的视图模型。

    • 如下定义视图模型:

       public class FuelLocationVM
       {
            public string Location { get; set; }
            public int Jan { get; set; }
            public int Feb { get; set; }
            public int March { get; set; }
       }
      
    • 返回虚拟机而不是匿名类型

          var res = (from loc in _context.Locations
             join Fl in _context.Fuels on loc.Name equals Fl.Locationn
             select new FuelLocation
                     {
                         Fuels = Fl,
                         Locations = loc
                     }).GroupBy(c => c.Fuels.Locationn)
            .Select(g => new FuelLocationVM
            {
                Location = g.Key,
                Jan = g.Where(c => c.Fuels.FMonth == 1).Sum(c => c.Fuels.Sale),
                Feb = g.Where(c => c.Fuels.FMonth == 2).Sum(c => c.Fuels.Sale),
                March = g.Where(c => c.Fuels.FMonth == 3).Sum(c => c.Fuels.Sale)
            }).ToList();
      
    • 在 razor 视图中引用 VM

          @model IList<COSAuthNew.Services.Generic.FuelLocationVM>
      <h2>Index</h2>
      <table class="table">
              <tr>
                  <th>LocationName</th>
                  <th>Jan</th>
                  <th>Feb</th>
                  <th>March</th>
                   <th>Total</th>
              </tr>
              @foreach (var item in Model)
              {
                  <td>@item.Location</td>
                  <td>@item.Jan</td>
                  <td>@item.Feb</td>
                  <td>@item.March</td>
                  <td>@(item.Jan + item.Feb + item.March)</td> 
              }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-19
      • 2018-08-18
      • 2010-10-27
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      • 1970-01-01
      相关资源
      最近更新 更多