簡體   English   中英

ASP.NET MVC - 對象不通過 Controller 到查看

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM