sagesky36
sagesky36

Reputation: 4692

Accessing data in a ViewModel

I have the following entity framework code snippet which has a "Groups" table and a child "ApplicationsGroupsLK" table that contains an ApplicationID field that I need.

IEnumerable<Groups> Groups = DbContext.Groups.Include("ApplicationsGroupsLK").Where(p => p.GroupNumber > 0);

The child data comes back obviously in a collection.

I basically need to display the parent data along with the child ApplicationID field (many Applications to 1 Group).

In my MVC View, what should my ViewModel look like that would contain the parent and child data coming back that I need in order that I can properly bind it to a grid?

Second post:

Further, the following model was generated from Entity Framework:

public partial class Project
    {
        public Project()
        {
            this.TimeTrackings = new HashSet<TimeTracking>();
        }

     [DataMember]
        public short ProjectID { get; set; }
     [DataMember]
        public short CustomerID { get; set; }
     [DataMember]
        public string Name { get; set; }
     [DataMember]
        public string Description { get; set; }
     [DataMember]
        public short CategoryID { get; set; }
     [DataMember]
        public short PriorityID { get; set; }
     [DataMember]
        public short StatusID { get; set; }
     [DataMember]
        public Nullable<decimal> Quote { get; set; }
     [DataMember]
        public string Notes { get; set; }
     [DataMember]
        public System.DateTime CreatedDate { get; set; }
     [DataMember]
        public Nullable<System.DateTime> UpdatedDate { get; set; }

     [DataMember]
        public virtual Category Category { get; set; }
     [DataMember]
        public virtual Customer Customer { get; set; }
     [DataMember]
        public virtual Priority Priority { get; set; }
     [DataMember]
        public virtual Status Status { get; set; }
     [DataMember]
        public virtual ICollection<TimeTracking> TimeTrackings { get; set; }
    }

You can see that TimeTrackings is the child table of Project. You can also see that CategoryID, CustomerID, PriorityID, and StatusID are foreign keys that the parent table has. In this case, I'm only interested in the CategoryID FK.

I haven't done this yet (not at my machine at home), but when I get the data into this model, what would actually be contained in the public virtual Category Category field? Since it's not a collection, what data is returned in this field after the query executes.

Third post: Telerik asp.net for mvc syntax for DB call:

IEnumerable<Groups> GroupList = db.GetGroups();

            return View(new GridModel<Groups>
            {
                Data = GroupList
            });

Fourth post: Trey, below is the code I modified for my purposes and was hoping you can check it over before I implement it. I think I undersand it and seems great...

public partial class Project
    {
        public Project()
        {
            this.TimeTrackings = new HashSet<TimeTracking>();
        }

     [DataMember]
        public short ProjectID { get; set; }
     [DataMember]
        public short CustomerID { get; set; }
     [DataMember]
        public string Name { get; set; }
     [DataMember]
        public string Description { get; set; }
     [DataMember]
        public short CategoryID { get; set; }
     [DataMember]
        public short PriorityID { get; set; }
     [DataMember]
        public short StatusID { get; set; }
     [DataMember]
        public Nullable<decimal> Quote { get; set; }
     [DataMember]
        public string Notes { get; set; }
     [DataMember]
        public System.DateTime CreatedDate { get; set; }
     [DataMember]
        public Nullable<System.DateTime> UpdatedDate { get; set; }
     [DataMember]
        public short ApplicationID { get; set; }
     [DataMember]
        public string ApplicationName { get; set; }
     [DataMember]
        public virtual Category Category { get; set; }
     [DataMember]
        public virtual Customer Customer { get; set; }
     [DataMember]
        public virtual Priority Priority { get; set; }
     [DataMember]
        public virtual Status Status { get; set; }
     [DataMember]
        public virtual ICollection<TimeTracking> TimeTrackings { get; set; }


     public ProjectModel(Project project)
     {
        ProjectID = project.ProjectID;
        CustomerID = project.CustomerID;
        Name = project.Name;
        Description = project.Description;
    CategoryID = project.CategoryID;
        PriorityID = project.PriorityID;
        StatusID = project.StatusID;
        Quote = project.Quote;
        Notes = project.Notes;
        CreatedDate = project.CreatedDate;
        UpdatedDate = project.UpdatedDate;
        ApplicationID = project.ApplicationsGroupsLK.ApplicationID;
        ApplicationName = project.ApplicationsGroupsLK.ApplicationName;
     }


     // Neat Linq trick to convert database query results directly to Model
     public static IList<ProjectModel> FlattenToThis(IList<Project> projects)
     {
        return projects.Select(project => new ProjectModel(project)).ToList();
     }

    }

Fifth post:

using (wmswebEntities DbContext = new wmswebEntities())
                {
                    DbContext.Configuration.ProxyCreationEnabled = false;
                    DbContext.Database.Connection.Open();

                    IEnumerable<Projects> projects = DbContext.Projects.Where(p => p.GroupNumber > 0);

                    IList<ProjectModel> results = Project.FlattenToThis(projects);

                    return results

}

Sixth post namespace CMSEFModel { using System; using System.Collections.Generic;

public partial class GroupModel
{
    public GroupModel()
    {
        this.ApplicationsGroupsLKs = new HashSet<ApplicationsGroupsLK>();
        this.GroupApplicationConfigurationsLKs = new HashSet<GroupApplicationConfigurationsLK>();
        this.UsersGroupsLKs = new HashSet<UsersGroupsLK>();
    }

    public int GroupNumber { get; set; }
    public string GroupName { get; set; }
    public int GroupRank { get; set; }
    public bool ActiveFlag { get; set; }
    public System.DateTime DateAdded { get; set; }
    public string AddedBy { get; set; }
    public System.DateTime LastUpdated { get; set; }
    public string LastUpdatedBy { get; set; }
    // Application - Lazy Loading population
    public int ApplicationID { get; set; }
    // UsersGroupsLK - Lazy Loading population
    public int UserNumber { get; set; }
    public string UserID { get; set; }

    public virtual ICollection<ApplicationsGroupsLK> ApplicationsGroupsLKs { get; set; }
    public virtual ICollection<GroupApplicationConfigurationsLK> GroupApplicationConfigurationsLKs { get; set; }
    public virtual ICollection<UsersGroupsLK> UsersGroupsLKs { get; set; }

    public GroupModel()
    {}
    public GroupModel(GroupModel group)
     {
        GroupNumber = group.GroupNumber;
        GroupName = group.GroupName;
        ActiveFlag = group.ActiveFlag;
        DateAdded = group.DateAdded;
        AddedBy = group.AddedBy;
        LastUpdated = group.LastUpdated;
        LastUpdatedBy = group.LastUpdatedBy;
        UserNumber = group.UsersGroupsLKs.
     }


 // Neat Linq trick to convert database query results directly to Model
 public static IList<GroupModel> FlattenToThis(IList<GroupModel> groups)
 {
    return groups.Select(group => new GroupModel(group)).ToList();
 }
}

}

Seventh Post - this is the model I am having trouble with about the errors I previously posted. Trey, if you could help, I WOULD REALLY APPRECIATE IT.... I'm "dead in the water" unless I can get this part working.

namespace CMSEFModel
{
    using System;
    using System.Collections.Generic;

    public partial class Group
    {
        public Group()
        {
            this.ApplicationsGroupsLKs = new HashSet<ApplicationsGroupsLK>();
            this.GroupApplicationConfigurationsLKs = new HashSet<GroupApplicationConfigurationsLK>();
            this.UsersGroupsLKs = new HashSet<UsersGroupsLK>();
        }

        public int GroupNumber { get; set; }
        public string GroupName { get; set; }
        public int GroupRank { get; set; }
        public bool ActiveFlag { get; set; }
        public System.DateTime DateAdded { get; set; }
        public string AddedBy { get; set; }
        public System.DateTime LastUpdated { get; set; }
        public string LastUpdatedBy { get; set; }
        // Application - Lazy Loading population
        public int ApplicationID { get; set; }
        // UsersGroupsLK - Lazy Loading population
        public int UserNumber { get; set; }
        public string UserID { get; set; }

        public virtual ICollection<ApplicationsGroupsLK> ApplicationsGroupsLKs { get; set; }
        public virtual ICollection<GroupApplicationConfigurationsLK> GroupApplicationConfigurationsLKs { get; set; }
        public virtual ICollection<UsersGroupsLK> UsersGroupsLKs { get; set; }

        public GroupModel(Group group)
         {
            GroupNumber = group.GroupNumber;
            GroupName = group.GroupName;
            ActiveFlag = group.ActiveFlag;
            DateAdded = group.DateAdded;
            AddedBy = group.AddedBy;
            LastUpdated = group.LastUpdated;
            LastUpdatedBy = group.LastUpdatedBy;
            UserNumber = group.UsersGroupsLKs.
         }

     // Neat Linq trick to convert database query results directly to Model
     public static IList<GroupModel> FlattenToThis(IList<Group> groups)
     {
        return groups.Select(group => new GroupModel(group)).ToList();
     }
    }

}

Eight post:

using (wmswebEntities DbContext = new wmswebEntities())
                {
                    DbContext.Configuration.ProxyCreationEnabled = false;
            DbContext.Configuration.LazyLoadingEnabled = true;
                    DbContext.Database.Connection.Open();

                    List<Groups> myGroups = new List<Groups>();

                     var myGroups = from p in DbContext.Groups
                                where p.ActiveFlag = true
                                select new
                                {
                                    p.Groups.ApplicationName,
                                    p.Groups.GroupName,
                                    p.Groups.GroupRank,
                                    p.Groups.ActiveFlag,
                                    p.Groups.DateAdded,
                                    p.Groups.AddedBy,
                                    p.Groups.LastUpdated,
                                    p.Groups.LastUpdatedBy,
                                    p.Groups.ApplicationsGroupsLK.ApplicationID,
                                    p.Groups.UsersGroupsLK.UserNumber
                                };

                    return myGroups;
                }

Upvotes: 0

Views: 480

Answers (1)

Trey Gramann
Trey Gramann

Reputation: 2004

This will take a slight tweak in thinking. The Grid will only accept a flat model. This type of question is asked often, here is a starter answer: Kendo UI Grid - How to Bind to Child Properties

If that does not help, post more of your code here and I can work through it with you.

Upvotes: 1

Related Questions