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:
(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.