eddyperu
asked on
MVC -- how to extract one value from a .CShtml view to another .CShtml view
I have this view and I am tying to add the genre.Albums in here but the value for it is NULL
GenreMenu.cshtml.
// the value for the albums Count needs to appear next to each genre.name.., how to do this?
Meanwhile, I have another View page, that give me what I need "Albums.Count()"
Browse.cshtml
How Can I extract the value from Albums.Count() and add it to GenreMenu.cshtml page?
thanks
GenreMenu.cshtml.
@model IEnumerable<MvcMusicStore.Models.Genre>
<ul id="categories">
@foreach (var genre in Model)
{
<li>@Html.ActionLink(genre.Name,
"Browse", "Store",
new { Genre = genre.Name }, null)
</li>
}
</ul>
// the value for the albums Count needs to appear next to each genre.name.., how to do this?
Meanwhile, I have another View page, that give me what I need "Albums.Count()"
Browse.cshtml
@model MvcMusicStore.Models.Genre
@{
ViewBag.Title = "Browse Albums";
}
<div class="genre" >
<ul id="album-list">
@foreach (var album in Model.Albums)
{
<li>
<a href="@Url.Action("Details", new { id = album.AlbumId })">
<img alt="@album.Title" src="@album.AlbumArtUrl" />
<span>@album.Title</span>
</a>
</li>
}
</ul>
</div>
How Can I extract the value from Albums.Count() and add it to GenreMenu.cshtml page?
thanks
What does your controller look like? This sounds like an issue with populating your model in the controller. Also, I do not see where you are trying to access genre.Albums in GenreMenu.cshtml
ASKER
my store controller looks like this:
using System;
using System.Collections.Generic ;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMusicStore.Models;
namespace MvcMusicStore.Controllers
{
public class StoreController : Controller
{
MusicStoreEntities storeDB = new MusicStoreEntities();
//
// GET: /Store/
public ActionResult Index()
{
var genres = storeDB.Genres.ToList();
return View(genres);
}
//
// GET: /Store/Browse?genre=Disco
public ActionResult Browse(string genre)
{
// Retrieve Genre and its Associated Albums from database
var genreModel = storeDB.Genres.Include("Al bums")
.Single(g => g.Name == genre);
return View(genreModel);
}
//
// GET: /Store/Details/5
public ActionResult Details(int id)
{
var album = storeDB.Albums.Find(id);
return View(album);
}
//
// GET: /Store/GenreMenu
[ChildActionOnly]
public ActionResult GenreMenu()
{
var genres = storeDB.Genres.ToList();
return View(genres);
}
}
}
using System;
using System.Collections.Generic
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMusicStore.Models;
namespace MvcMusicStore.Controllers
{
public class StoreController : Controller
{
MusicStoreEntities storeDB = new MusicStoreEntities();
//
// GET: /Store/
public ActionResult Index()
{
var genres = storeDB.Genres.ToList();
return View(genres);
}
//
// GET: /Store/Browse?genre=Disco
public ActionResult Browse(string genre)
{
// Retrieve Genre and its Associated Albums from database
var genreModel = storeDB.Genres.Include("Al
.Single(g => g.Name == genre);
return View(genreModel);
}
//
// GET: /Store/Details/5
public ActionResult Details(int id)
{
var album = storeDB.Albums.Find(id);
return View(album);
}
//
// GET: /Store/GenreMenu
[ChildActionOnly]
public ActionResult GenreMenu()
{
var genres = storeDB.Genres.ToList();
return View(genres);
}
}
}
ASKER
At the end it will need to looks like something like this attachment . where I need to add the list count for each genre.
genre.png
genre.png
Does this show your content:
@model IEnumerable<MvcMusicStore.Models.Genre>
<ul id="categories">
@foreach (var genre in Model)
{
<li>@Html.ActionLink(genre.Name,
"Browse", "Store",
new { Genre = genre.Name }, null)
@genre.Albums?.Count()
</li>
}
</ul>
Notice that I added: @genre.Albums?.Count()
ASKER
This is how the result looks like, when I add your code
Rock ?.Count()
Classical ?.Count()
Jazz ?.Count()
Pop ?.Count()
Disco ?.Count()
Latin ?.Count()
Metal ?.Count()
Alternative ?.Count()
Reggae ?.Count()
Blues ?.Count()
Rock ?.Count()
Classical ?.Count()
Jazz ?.Count()
Pop ?.Count()
Disco ?.Count()
Latin ?.Count()
Metal ?.Count()
Alternative ?.Count()
Reggae ?.Count()
Blues ?.Count()
ASKER
I tried without the symbol "?", just in case was a typo and I get an error for that line
Value cannot be null.
Parameter name: source
Value cannot be null.
Parameter name: source
ASKER
The Album is NUll at this point. It will start populating after the client click the link(genre name) and it will go to here(code below), where the album is populated from store DB. After this point we will have data in album.
public ActionResult Browse(string genre)
{
// Retrieve Genre and its Associated Albums from database
var genreModel = storeDB.Genres.Include("Al bums")
.Single(g => g.Name == genre);
return View(genreModel);
}
public ActionResult Browse(string genre)
{
// Retrieve Genre and its Associated Albums from database
var genreModel = storeDB.Genres.Include("Al
.Single(g => g.Name == genre);
return View(genreModel);
}
Oh I see. You might need to include the Albums when loading the genres in the controller
public ActionResult GenreMenu()
{
var genres = storeDB.Genres.Include(g=>g.Albums).ToList();
return View(genres);
}
ASKER
Error , after getting your code :
Can not convert lambda expression to type string because it is not a delagate type'
Can not convert lambda expression to type string because it is not a delagate type'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Provided a solution to the question.