.net mvc 3 passing related classes from the controller
God my head is killing me.
Ok I have a controller where i want to pass nested data to the view. Which I am doing like so:
namespace helpme.mvc.Controllers
{
public class CategoryController : Controller
{
private HelpMeContext db = new HelpMeContext();
public ViewResult Index()
{
var model = db.Category.Include(c => c.SubCategories).ToList();
return View(model);
}
}
}
But it is not working. SubCategories come out empty even though there are rows in them. Any suggestions?
Using a break point i see that the model is being correctly populated, but nothing is displayed in the view and no error msg.
Here is the view code:
开发者_JS百科@model IEnumerable<helpme.mvc.Models.Category>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@Model.First().SubCategories.First().Name // UPDATE, THIS DISPLAYS A VALUE, SO WHY DOES THE CODE BELOW JUST DISPLAY THE FIRST LEVEL (Categories)?
<ul>
@foreach (var c in Model) {
<li>
<ul>
@foreach (var sc in c.SubCategories)
{
@Html.Display(sc.Name)
foreach (var ssc in sc.SubSubCategories)
{
@Html.Display(ssc.Name)
}
}
</ul>
</li>
}
</ul>
For some reason it only displays the first level, as if it did not receive the SubCategories, even though the break-point proves that it did.
And here is the model, just for reference:
public class Category
{
public Category()
{
SubCategories = new List<SubCategory>();
}
public int ID { get; set; }
[StringLength(255, MinimumLength = 1)]
public string Name { get; set; }
public ICollection<SubCategory> SubCategories { get; set; }
}
public class SubCategory
{
public SubCategory()
{
SubSubCategories = new List<SubSubCategory>();
}
public int ID { get; set; }
[Required()]
[StringLength(255, MinimumLength = 1)]
public string Name { get; set; }
public Category Category { get; set; }
public ICollection<SubSubCategory> SubSubCategories { get; set; }
}
Perhaps explicitly type define your model
as List<Category>
. Suspect it's currently ObjectQuery<Category>
? Can you confirm?
As you know, your View is expecting a IEnumerable<helpme.mvc.Models.Category>
.
Try explicitly casting your model to the type your View wants.
List<Category> model = db.Category.Include(c => c.SubCategories).ToList();
It's not clear where the root of the problem is: in the data access, or in accessing the objects in the View. Can you load up some dummy Category and SubCat in that method, and pass known good values to your View? That'll help determine where the issue lies.
I would guess that .Include().ToList()
is probably not doing what you think it's doing.
SubCategories probably aren't being mapped to the Category objects you pass to the view.
Try mapping them manually with view models:
e.g.
public class PageViewModel
{
public PageViewModel()
{
this.Categories = new List<Category>();
}
public IEnumerable<Category> Categories {get;set;}
}
...etc
And in your code:
var viewModel = new PageViewModel();
foreach (var category in db.Categories)
{
var categoryVM = new CategoryViewModel();
foreach (var subcategory in category.SubCategories)
{
categoryVM.SubCategories.Add(....
}
}
...etc
Do only one level first (to SubCategory) to confirm.
Try to replace
@Html.Display(sc.Name)
with
@sc.Name
Also please remember you can always put breakpoints in view code, and see how the rendering goes. Hope you find this useful. -Zs
精彩评论