私はこの問題にあまりにも長い間立ち往生しており、助けていただければ幸いです。
ビューでは、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());