MVC List Issues

Stanton_Roux
Stanton_Roux used Ask the Experts™
on
Hi There

I have two tables Person and Tracks
One person can have multiple tracks

I am having an issue getting a list of tracks for a person.

I have tried it a couple of ways but still no luck.

Could you please check my code and help me out.

 

The code below does not return any tracks

 

TracksController

 

    [Authorize]

        public ActionResult List()

        {

            var authperson = _authenticationService.UserLogin("Stan", "Stan");

            var tracklist = _trackService.GetAllTracksByPerson(authperson.Id);

            var viewmodel = new TracksViewModel(tracklist);

            return View("List", viewmodel);

        }

 

 

TracksViewModel

 

public class TracksViewModel

    {

       

     

        public Guid Genre { get; set; }

        public IList<Track> Tracks { get; set; }

        public Guid PersonId { get; set; }

        public SelectList GenreOptions { get; set; }

 

 

        public TracksViewModel(IList<Genre> genres)

        {

            GenreOptions = new SelectList(genres, "Id", "Name");

 

           

        }

 

        public TracksViewModel(IList<Track> tracks)

        {

            Tracks = new List<Track>(tracks);

 

        }

 

Tracks Service

 

public IList<Track> GetAllTracksByPerson(Guid personid)

        {

            using (new UnitOfWorkScope())

            {

                return _trackRepository.Select(x => x.Person.Id == personid).ToList() as IList<Track>;

            }

        }

 

I have also tried the following but it gives me a lazy load error.

 

 

    [Authorize]

        public ActionResult List()

        {

            var authperson = _authenticationService.UserLogin("Stan", "Stan");

            var tracklist = authperson.Tracks;

            var viewmodel = new TracksViewModel(tracklist);

            return View("List", viewmodel);

        }

 

Entity

 

public class Person:EntityBase

    {

 

        private readonly IList<Track> _tracks = new List<Track>();

 

        public Person()

        {

 

        }

        public Person(string firstname,

                     string surname,

                     string emailaddress,

                     string contactnumber,

                     string username,

                     string password)

        {

            FirstName = firstname;

            EmailAddress = emailaddress;

            ContactNumber = contactnumber;

            UserName = username;

            Password = password;

            Surname = surname;

        }

 

        public virtual void AddTrack(Track track)

        {

            track.Person = this;

            _tracks.Add(track);

        }

 

        public virtual string FirstName { get; set; }

        public virtual string EmailAddress { get; set; }

        public virtual string ContactNumber { get; set; }

        public virtual string UserName { get; set; }

        public virtual string Password { get; set; }

        public virtual string Surname { get; set; }

       

        public virtual IList<Track> Tracks

        {

            get { return _tracks.ToList().AsReadOnly(); }

        }

 

       

 

       

    }

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Unless I'm missing something silly, I'm not sure this can be gleaned from the code provided.

How do you know you're not getting tracks? because they're not displayed, or because the results is an empty list? Have you done a break point in GetAllTracksByPerson to see what it's fetching? I can't see how the repository is implemented, but again ... this seems like something you really need to step into to see the original point of "failure".

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial