Reputation: 2307
I have this in my View:
@{
var categories = (List<C_Category>)Model.c;
}
@foreach (C_Category cat in categories)
{
<option value="@cat.C_Id">@cat.C_Name</option>
}
And this in my 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 });
}
}
But in my view I get an error:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'c'
I'm sure I've been doing this way all the time, am I missing something?
Here is the addwatch:
Error:
Upvotes: 0
Views: 77
Reputation: 40
if u declare AdminObj
as model in view than you have to pass var ao in return perameter
or
currently u are returning anonymous object
as model in return view which is not work in view as you casted
mention what you added as @model in view
Had to add this in the view as mentioned in this answer.
@model ProjectWeb.Controllers.HomeController.AdminObj
And in 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);
}
}
Upvotes: 1