Link to home
Start Free TrialLog in
Avatar of eddyperu
eddyperuFlag for United States of America

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.

@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>

Open in new window


// 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>

Open in new window



How Can I extract the value from Albums.Count() and add it to GenreMenu.cshtml page?
thanks
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

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
Avatar of eddyperu

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("Albums")
                .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);
        }

    }
}
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
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>

Open in new window

Notice that I added: @genre.Albums?.Count()
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()
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
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("Albums")
                .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);
}

Open in new window

Error , after getting your code :
   Can not convert lambda expression to type string because it is not a delagate type'
ASKER CERTIFIED SOLUTION
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Provided a solution to the question.