[英]ASP.NET MVC - Objects not passing through Controller to VIew
我的視圖中有這個:
@{
var categories = (List<C_Category>)Model.c;
}
@foreach (C_Category cat in categories)
{
<option value="@cat.C_Id">@cat.C_Name</option>
}
這在我的 Controller 中:
[HttpGet]
public ActionResult Admin()
{
using (var context = new sopingadbEntities())
{
List<P_Product> p = context.P_Product.OrderByDescending(x => x.P_Id).ToList();
List<C_Category> c = context.C_Category.ToList();
var ao = new AdminObj()
{
p = p,
c = c
};
return View("Admin", new { c, p });
}
}
但在我看來,我得到一個錯誤:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:“對象”不包含“c”的定義
我確定我一直在這樣做,我錯過了什么嗎?
這是addwatch:
錯誤:
如果您在視圖AdminObj
聲明為 model,那么您必須在返回參數中傳遞var ao
或者
目前,您在返回視圖中返回anonymous object
作為 model ,這在您投射時不起作用
提及您在視圖中添加為@model 的內容
必須在此答案中提到的視圖中添加此內容。
@model ProjectWeb.Controllers.HomeController.AdminObj
在 Controller 中:
[HttpGet]
public ActionResult Admin()
{
using (var context = new sopingadbEntities())
{
List<P_Product> p = context.P_Product.OrderByDescending(x => x.P_Id).ToList();
List<C_Category> c = context.C_Category.ToList();
var ao = new AdminObj()
{
p = p,
c = c
};
return View(ao);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.