0

I am trying to learn ASP.NET MVC, I some knowledge with ASP.NET Webforms, but the company I am going to work with in the future wants me to learn MVC so I am trying to "migrate" one of my older Webforms projects over to MVC. Just to learn the pattern and the difference.

I am doing Database First, since I already had the database from before, let Visual Studio convert it into ADO.NET Data Model with the Model Classes and the DB "context file".

My problem is that I am trying to pass several "models" to View with LINQ. My Database looks like this: myDB
(source: danielpc.dk)

My older old working SQL syntax looks like this:

SELECT 
TOP 6
    Performance.PerformanceId,
    Performance.ActId, 
    Performance.[Date],

    Act.Title, 
    Act.[Description],
    Act.Imageurl,

    Stage.Stagename,

    Artistname.Artistname

FROM 
    Performance

       JOIN Act ON Performance.ActId=Act.ActId
       JOIN STAGE ON Performance.Stage=Stage.StageId
       JOIN [User] ON Act.ArtistId=[User].UserId
       JOIN Artistname ON [USER].UserId=Artistname.ArtistId 

       WHERE Performance.[Date] > GETDATE()

       ORDER BY YEAR(Performance.[Date])  

I just don't know how to pass this to the view, I can pass the PerformanceID, ActID and Date, since they all are properties from Performance, but how do I add the rest?

Here is my Controller:

public ActionResult Index()
        {
            var q = (from Performance in db.Performance
                     join Stage in db.Stage on new { Stage = (int)Performance.Stage } equals new { Stage = Stage.StageId }
                     join Artistname in db.Artistname on new { UserId = Performance.Act.User.UserId } equals new { UserId = Artistname.ArtistId }
                     where
                       Performance.Date > SqlFunctions.GetDate()
                     orderby
                       SqlFunctions.DatePart("year", Performance.Date)
                     select new
                     {
                         Performance.PerformanceId,
                         Performance.ActId,
                         Performance.Date,
                         Performance.Act.Title,
                         Performance.Act.Description,
                         Performance.Act.Imageurl,
                         Stage.Stagename,
                         Artistname.Artistname1
                     }).Take(6).ToList();

            var performance = new List<Performance>();

            foreach (var per in q)
            {


                performance.Add(new Performance()
                {
                    PerformanceId = per.PerformanceId,
                    ActId = per.ActId,
                    Date = per.Date

                    // TODO : add Title, Description, Imageurl, stagename, artistname
                });
            }

            return View(performance);
        }

Thanks in regards, let me know if you need more information.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Sigils
  • 2,492
  • 8
  • 24
  • 36
  • For this I believe your need to create a different model which has all required properties that you have to send to the view. Instead of using the entity, try creating a ViewModel with all the properties you want and name it something like PerformanceViewModel. You can also use `Select new PerformanceViewModel` directly instead of looping into the result set again. – Nilesh Feb 28 '14 at 11:21
  • Thank you so much sir! It worked! :) So you would recommend to make ViewModels every time you have to join tables from 2+ different classes? – Sigils Feb 28 '14 at 11:56
  • Yes Sir, Thats the only way out. For direct tables you can use the entities directly, but I dont think that you should expose your database entities in the Web Layer. – Nilesh Feb 28 '14 at 12:01
  • I am really glad you are helping me and giving me some extra tricks! Btw what do you mean with 'but I dont think that you should expose your database entities in the Web Layer'? – Sigils Feb 28 '14 at 12:16
  • you will get to know better at this [link](http://stackoverflow.com/questions/821276/why-should-i-isolate-my-domain-entities-from-my-presentation-layer) – Nilesh Feb 28 '14 at 13:23

0 Answers0