Chris Barry
Chris Barry

Reputation: 4594

Building custom shaped view model for asp mvc framework

I am building my first few projects with ASP.NET MVC, and I have a list of items that come from 2 tables.

Using the nerd dinner example, this is my problem.

Lets say I want to have all the dinners, with all the rsvp information in a list on one page.

So it would go

dinnername1

rsvp1 rsvp2

dinnername2

rsvp1 rsvp2 rsvp3

Currently I'm using this linq to bring what I think is the correct data back

public IQueryable<Dinner> GetAllUserDinnersAndRSVPs(string userId)
    {
        return from dinner in db.Dinner
               from rsvp in db.RSVP
               where dinner.userId == userId
               where rsvp.userId == userId
               select dinner;
    }

This I think is bringing back the correct number of results, but because I am only bringing back the dinner type, the rsvp information doesn't show up in the collection.

So I think what I need to do is build a custom Model (class?) which will store all the info from the dinners table and the rsvp table, called something like DinnerTask.cs

This will basically just have a load of properties for each of the columns of both of the tables, and will mean that I can sort the entire list in c# before using a foreach to render out to the page.

I guess the defination of the class would be something like this.

public class DinnersAndRSVPsModel
{


        public int DinnerId { get; set; }
        public string DinnerName { get; set; }
        rest of fields for table 1......

        public int RSVPId { get; set; }
        public int RSVPInfo { get; set; }
        rest of fields for table 2.......
}

I'm asking this question because I think this seems like I am doing this wrong, and hopefully someone can point me in the right direction for working with data like this.

I would really appreciate any examples of this including the linq query, class defination etc... Bonus points if you could help me with any of the sorting as well, but I think I should be able to work that out.

Thank you for any help.

Upvotes: 0

Views: 1301

Answers (2)

Chris Barry
Chris Barry

Reputation: 4594

I read Linq in Action to help me get to this solution, and then this post also helped me greatly

How do I group data in an ASP.NET MVC View?

The way I got this to work was by adding a custom type (class)

(excuse the bad naming conventions)

public class DinnerAndRsvpName
{
    public string Dinnername { get; set; }
    public string RSVPname { get; set; }

}

I then rewrite the linq query to this

return from dinner in db.Dinner
               join rsvp in db.RSVPs on Dinner.id equals RSVP.DinnerId into DinnerRsvp
               where dinner.userId == userId
               from rsvp in DinnerRsvp.DefaultIfEmpty()
               select new DinnerAndRsvpName
               {
                   Dinnername = goal.name,
                   RSVPname = rsvp == default(RSVPs) ? "No RSVP" : rsvp.name

               };

and then changed the model view to this

<ul>
 <% foreach (var group in Model.GroupBy(item => item.Dinnername)) { %>

<li><%= Html.Encode(group.Key) %>
 <ul>

 <% foreach (var item in group) { %>
   <li><%= Html.Encode(item.RSVPname) %></li>  
 <% } %>

 </ul>

  <% } %>

Upvotes: 1

Joseph
Joseph

Reputation: 25523

I would imagine that the object model would already be structured in such a way that a Dinner would have a list of RSVPs. I could be wrong ( I haven't looked at the NerdDinner example), but if that is indeed the case then all you need to do is specify the load options for your dinner to include the RSVPs.

It would look something like this:

public IQueryable<Dinner> GetAllUserDinnersAndRSVPs(string userId)
{
    DataLoadOptions options = new DataLoadOptions();
    options.LoadWith<Dinner>(d => d.RSVPs); //whatever property has the RSVPS
    db.LoadOptions = options;

    return from dinner in db.Dinner
           where dinner.userId == userId
           select dinner;
}

Upvotes: 0

Related Questions