Saturday, January 9, 2016

Parent Child in MVC

Given your structure of Albums and Tracks, it would probably be preferable to have a direct 1 to many relationship between Albums and Tracks so that 1 album can have many tracks. Regardless of how many albums a track may appear on, your models could look like:

public class Albums {
    public int ID { get; set; }
    public string Artist { get; set; }
    public string AlbumTitle { get; set; }
    public string Notes { get; set; }
    public IQueryable<Track> Tracks { get; set; }
}

public class Track {
    public int ID { get; set; }
    public int AlbumID { get; set; }    // This may be unnecessary
    public string TrackTitle { get; set; }
    public string Notes { get; set; }
}
This would allow your data layer to populate all of the tracks that belong to a specific Album, but if you don't need to know the tracks it can just remain null. But your data layer just needs to fetch the album and then populate the Tracks property with an IQueryable object.

public ViewResult AlbumExt(int id)
{
    Album album = albumRepository.All.Where(a => a.ID == id);
    album.Tracks = tracksRepository.All.Where(t => t.AlbumId == id);
    return View(album);
}
This would give you a single model, but all of the related tracks would still be accessible through the Model.Tracks property in the view:

@model MyNameSpace.Album

@{
    foreach (var track in Model.Tracks)
    {
        <text>
            <h1>@track.TrackTitle</h1><br />
        </text>
    }  
}



Example 2


MVC: Multiple Models in One Single View
Here the question is say you have two model M1 and M2. M1 model consistd properties like a,b. M2 Consists properties like c,d. All a,b,c, d are required(Tagged as [Required]). Now you have a single View V. You have to display all a,b,c,d four properties into V view.

The simplest solution is first create M1 and M2 model class with a,b[in M1] and c,d[in M2] property . Now in M1 Model have a list property of M2 class

public List<M2> M2Prop { get; set; }

Access M1 Class in V view. You are done. Have a look on the below sample code.

public class Student
    {
        [Required]
        public int RollNo { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        public string Class { get; set; }

        public string Address { get; set; }

        public List<StudentScore> StudentScore { get; set; }

   }

And the ‘StudentScore’ class is as follows.

public class StudentScore
    {
        [Required]
        public int StudentRollNo { get; set; }

        [Required]
        public string StudentName { get; set; }

        [Required]
        public string Subject { get; set; }

        [Required]
        public int Marks { get; set; }

        [Required]
        public string Grade { get; set; }

    }

Create View with strongly typed of Student Class.
And on the view it is like below.

@model IEnumerable<MultipleModelInOneView.Models.Student>


@foreach (var item in Model)
    {

<td>
                @item.RollNo
            </td>
            <td>
                @item.Name
            </td>

       @foreach (var score in item.StudentScore)
            {
                 <td>
                    @score.Grade
               
                </td>
                <td>
                    @score.Marks
                </td>
         
            }

}


No comments:

Post a Comment