我一直在這個問題上停留太久,會愛上一些幫助。傳遞來自控制器的查詢數據以查看
在視圖中,人們可以從兩個單選按鈕列表中選擇兩個項目,這兩個項目通過FormMethod.Get返回到HomeController中的Index事件。
這兩個值'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 }
}
的HomeController
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());
你可以添加一些請查看代碼 - 你不應該使用查詢字符串一樣,在MVC它失敗的目的,你應該加入表單將對象返回到控制器的參數。我會建議你通過這個教程 - http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-1 – Rob
嗨Rob這是相關的視圖代碼,它只是一個簡單的foreach,但我想要item.Price,Item.Item等 – netchicken