MVC View - how to display the "category" of a collection of objects, not just the objects themselves
Using ASP MVC and Entity Framework. In a view you have a page declaration that defines the model for that view, there will be a collection that implements IEnumerable. Let's say the collection contains car objects that only belong to Ford (Ford - Category).
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<detelete.Models.Car>>" %>
This list of only Ford vehicles was generated using the LINQ to Entities query. The EF object is aware of the car manufacturer relationship (which I call the category)
var dat = ent.CarSet.Where(m => m.Manufacturer.Name == nm);
List<Car> cars = dat.ToList<Car>();
return View("ListingByManufacturer", cars);
So, in the view, I show a list of cars that are all Fords. I have a view that displays all the properties of the cars correctly, but there is no way to show which category (manufacturer in this example) the cars are from . I've seen some EF / MVC examples that have two foreach loops and the top one displays the producer - but that feels klugey.
It looks like it should be easy, but I'm stuck ...
a source to share
You can simply pass the manufacturer name through ViewData
in the controller:
ViewData["Manufacturer"] = nm;
Or use another object as a model containing both cars and manufacturer name
public class CategoryViewModel {
public string ManufacturerName { get; set; }
public List<Car> Cars { get; set; }
}
And pass an instance of this class to the view.
a source to share