0

私はこの問題にあまりにも長い間立ち往生しており、助けていただければ幸いです。

ビューでは、FormMethod.Get を介して HomeController の Index イベントに返される 2 つのラジオボタン リストから 2 つの項目を選択できます。

これらの 2 つの値、'parts と 'use' は照会されて結果が返され、viewbag を介してビューに返されます。ただし、viewbag はビューで { Item = Kona, Price = 400.0000, Quantity = 2 } のような行を返します。

item.Item、Item.Priceなどの各アイテムを個別に使用できるように戻したいのですが。

私は見つけることができるすべてのものを試しましたが、役に立ちませんでした。匿名クラスのアイテムも赤いエラーをスローします

意見

foreach(var item in ViewBag.getstock)
{ //Show it and then make a new line with the <BR/>
    @item < br / >
    //{ Item = Kona, Price = 400.0000, Quantity = 2 } 
}

ホームコントローラー

 public ActionResult Index()
 {
     //this returns the entire query string
     ViewBag.querystring = Request.QueryString;

     //if any keys are in the url from the view
     if (Request.QueryString.HasKeys())
     {
         //extract them out so that you can use them
         //key = the name such as Part or Use it goes Key & Value this is passed to a Viewbag
         //get(o) is getting the value at place 0, the first value in the url

         ViewBag.querystringvalue0 = Request.QueryString.Get(0);
         ViewBag.querystringvalue1 = Request.QueryString.Get(1);
     }

     //if there is any query string
     if (Request.QueryString.HasKeys())
     {
         //pass the data to a couple of variables,
         var parts = Request.QueryString.Get(0);
         var use = Request.QueryString.Get(1);

         //put them in a new query and return the results
         ViewBag.getstock = from p in Bikeshopdb.Stocks
         where p.PartName == parts && (p.Road == use || p.Mtn == use || p.Hybrid == use) select new
         {
             p.Item, p.Price, p.Quantity
         };

     }
     return View(Bikeshopdb.Stocks.ToList());
4

1 に答える 1

0

ViewModel クラスを使用してクエリ結果を保持し、ビューに戻します。例えば:

ホームコントローラー

public class MatchingStock()
{

    public int ID { get; set; }
    public string Item { get; set; }
    public int Price { get; set; }
    public int Quantity { get; set; }

}

public ActionResult Index()
{

    //...

    var list = 
        (from p in Bikeshopdb.Stocks
        where p.PartName == parts && 
            (p.Road == use || p.Mtn == use || p.Hybrid == use)
        select new MatchingStock() { 
            ID = p.ID, 
            Item = p.Item, 
            Price = p.Price, 
            Quantity = p.Quantity}).ToList();

    ViewBag.getstock = list;

    //...

}

意見

@foreach (var item in (List<MatchingStock>)ViewBag.getstock)
{
    @item.Item @item.Price @item.Quantity
    <br />
}
于 2012-11-03T01:37:37.950 に答える