enter image description here我有一個控制器,從用戶定義的函數使用實體framwework.I我試圖只顯示我的數據在視圖中,並填充我的表中的數據。在視圖中顯示從控制器的列表MVC
我的控制器看起來是這樣的:
public ActionResult Index()
{
var description = "Toyota";
QuotingEngineEntities1 vehicles = new QuotingEngineEntities1();
List<object> list = new List<object>();
using (var context = new QuotingEngineEntities1())
{
var vehicle = from s in context.fn_GetVehicle(description)
select new
{
s.MAKE,
s.MODEL,
s.PRICE,
s.POWER,
s.Transmission
};
list.Add(vehicle.FirstOrDefault());
}
ViewBag.list = list;
return View(ViewBag.list);
}
和我的看法是這樣的
@foreach (var v in ViewBag.list)
{
<li>@v.MODEL</li> //i get an error
<br />
}
我終於得到它的幹活。通過數據不得不循環將其添加到列表中。
public ActionResult Index()
{
var description = "Toyota";
List<fn_GetVehicle_Result> list = new List<fn_GetVehicle_Result>();
using (var context = new QuotingEngineEntities1())
{
var query = context.fn_GetVehicle(description);
foreach (var v in query)
{
list.Add(v);
}
ViewBag.list = list;
}
return View("Index",ViewBag.list);
}
enter code here
'
視圖的名稱是什麼?您需要傳遞視圖名稱,然後傳遞您將要傳遞的變量或數據。 '返回查看(「查看」,ViewBag.list);' – Joseph
@約瑟夫我認爲這不是強制性的'返回'聲明'viewbag'是不是? – Curiousdev