我一直在这个问题上停留太久,希望得到一些帮助。
在视图中,人们可以从两个单选按钮列表中选择两个项目,这两个单选按钮列表通过 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 }
}
家庭控制器
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());