2017-09-15 5 views
-2

このメソッドは、最も人気の高い/お気に入りの製品と各製品の数量の合計に基づく降順を表示する必要があります。それはまだテストする必要がありますが。私はこのメソッドをProductPop()と呼ぶ必要があります。Controller.MVCから渡されたビュー内のアイテムのリストを表示する方法

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

がある場合:これはこれは私が私の見解で持っているものである

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

コントローラにリンクされていない(このビューは、単にビューを返し、別のコントローラの空のIndexメソッドです)任意の参照/アドバイス。私はGladyがそれを感謝します。ありがとう!あなたはinstanを作成すべきではない

+1

あなたは基本ASP.NET MVCのチュートリアルを読むことを試みましたか? – Marusyk

答えて

0

そのようなコントローラのceは、むしろあなたのビューにモデルに沿って渡します。 List<Order_Detail>をモデルとして直接渡すことも、下のコードサンプルにあるようにList<Order_Detail>をモデルにラップすることもできます。

まず、あなたのモデルのための新しいクラスを作成します。

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

に注意してください。私が指摘したところでは、あなたがGroupBySelectの操作を実行しているので、List<Order_Detail>にあなたのresultが常に空であると指摘しています。

続いて最後に、あなたのビューでは、@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> 
} 
1

リターンOrderDetailインデックスのActionResultのデータやビューに@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> 
} 
関連する問題