【问题标题】:How to display a list of items with in view passed from controller.MVC如何显示从控制器传递的视图中的项目列表。MVC
【发布时间】:2017-09-15 21:17:23
【问题描述】:

我有这个方法应该显示最受欢迎/最喜欢的产品,并根据每个产品的数量总和降序排列。虽然它仍然需要测试。我需要将此方法ProductPop() 从控制器调用到视图中。那是没有链接到控制器的

public class AnalysisController : Controller
{
    public List<Order_Detail> ProductPop()
    {
        List<Order_Detail> result = new List<Order_Detail>();
        result.GroupBy(l => l.ProductName).Select(cl => new Order_Detail
        {
             ProductName = cl.First().ProductName,
             Quantity = cl.Sum(c => c.Quantity)
        }).OrderByDescending(k=>k.Quantity).ToList();

        return result;
    }
}

这就是我的视图中的内容(这个视图是另一个控制器的空索引方法,它只返回一个视图):

@using ProjectName.Controllers;
@{
    AnalysisController AC = new AnalysisController();
    ViewBag.Title = "Index";
}

@foreach(var od in ProductPop())
{
   <table>
      <tr>
          <th>
              Product Name
          </th>
          <th>
             Quantity
          </th>
      </tr>
       <tr>
           <td>
               @od.ProductName
           </td>
           <td>
               @od.Quantity
           </td>
       </tr>
   </table>
}

如果有任何参考/建议。我会很感激的。谢谢!

【问题讨论】:

  • 你试过阅读基础 ASP.NET MVC 教程吗?

标签: c# asp.net-mvc list razor lambda


【解决方案1】:

在 Index ActionResult 中返回 OrderDetail 数据并在您的视图中使用 @model

控制器

[HttpGet]
public ActionResult Index()
{
   return View(ProductPop());
}

查看

@model Order_Detail;

@foreach(var od in ProductPop())
{
   <table>
      <tr>
          <th>
              Product Name
          </th>
          <th>
             Quantity
          </th>
      </tr>
       <tr>
           <td>
               @model.ProductName
           </td>
           <td>
               @model.Quantity
           </td>
       </tr>
   </table>
}

【讨论】:

    【解决方案2】:

    您不应该像那样创建控制器的实例,而应该将模型传递给您的视图。您可以将List&lt;Order_Detail&gt; 直接作为模型传递,或者将List&lt;Order_Detail&gt; 包装在一个模型中,就像我在下面的代码示例中那样。

    首先,为您的模型创建一个新类:

    public class ProductPopModel {
        public List<Order_Detail> Result { get; set; }
    }
    

    然后,在 Analysis 控制器内的 Index 操作中,创建一个 ProductPopModel 实例并填充它:

    public class AnalysisController : Controller
    {
        public ActionResult Index() {
            List<Order_Detail> result = new List<Order_Detail>();
            //The code below will never return anything because you are peforming operations on an empty list
            result.GroupBy(l => l.ProductName).Select(cl => new Order_Detail
            {
                 ProductName = cl.First().ProductName,
                 Quantity = cl.Sum(c => c.Quantity)
            }).OrderByDescending(k=>k.Quantity).ToList();
            var model = new ProductPopModel {
                Result = result
            };
    
            return View(model);
        }
    }
    

    请注意我指出您的result 将始终为空的评论,因为您正在对空的List&lt;Order_Detail&gt; 执行操作GroupBySelect

    最后,在您看来,使用@model 指定模型类型:

    @model ProductPopModel
    @{
        ViewBag.Title = "Index";
    }
    
    @foreach(var od in Model.Result)
    {
       <table>
          <tr>
              <th>
                  Product Name
              </th>
              <th>
                 Quantity
              </th>
          </tr>
           <tr>
               <td>
                   @od.ProductName
               </td>
               <td>
                   @od.Quantity
               </td>
           </tr>
       </table>
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      相关资源
      最近更新 更多