Reputation: 41
i want to get the name of model genre and title of model list in the partial view but @genre.Lists.Title doesn't work this is my genre model
public class Genre
{
public int GenreId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<List> Lists { get; set; }
}
and this is my List model
[Bind(Exclude = "ListId")]
public class List
{
[ScaffoldColumn(false)]
public int ListId { get; set; }
[DisplayName("Genre")]
public int GenreId { get; set; }
[DisplayName("Maker")]
public int MakerId { get; set; }
[Required(ErrorMessage = "An List Title is required")]
[StringLength(160)]
public string Title { get; set; }
[Required(ErrorMessage = "Price is required")]
[Range(0.01, 100.00,ErrorMessage = "Price must be between 0.01 and 100.00")]
public decimal Price { get; set; }
[DisplayName("List URL")]
[StringLength(1024)]
public string ListUrl { get; set; }
public Genre Genre { get; set; }
public Maker Maker { get; set; }
public virtual List<OrderDetail> OrderDetails { get; set; }
}
and this my actionResult
public ActionResult Navbar()
{
var genres = storeDB.Genres.Include("Lists").ToList();
return PartialView("Navbar",genres);
}
and this is my PartialView
@model IEnumerable<Store.Models.Genre>
@foreach (var genre in Model)
{
@genre.Name
@genre.Lists.Title
}
Upvotes: 1
Views: 334
Reputation: 4039
@genre.Lists
is of type List<List>
, not List
(by the way, I would rename your class somehow, it's easy to confuse with the standard library class of this name).
So you either need another foreach
loop to iterate over @genre.Lists
or you can get the first element with @genre.Lists[0].Title
. It's up to you what you actually want to achieve. For example, you could use string.Join
:
@model IEnumerable<Store.Models.Genre>
@foreach (var genre in Model)
{
<text>
@genre.Name
@string.Join(", ", genre.Lists.Select(x => x.Title))
</text>
}
Or write some real HTML. Again, depends what you want your output to be.
Upvotes: 2