views:

206

answers:

2

Hi there

I am quite new with this MVC in ASP.NET. If I have a page which has Tournament class (in Detail mode) and underneath it has a list of PlayRoundHoles class which it's comes from stored procedure due to a complex quiry.

How do I achive this to display this list under Tournament Detail View? I don't quite understand where this PlayRoundHoles sits in the Controller as well as the View?

Thanks

A: 

You can pass instance of Tournament to View. And then render PlayRoundHoles in View.

for example see here http://stephenwalther.com/blog/archive/2009/04/13/asp.net-mvc-tip-50-ndash-create-view-models.aspx

Trickster
I had a look on that one. In my case, PlayRoundHoles sits under Tournament Detail View. So it's like a sub view maybe?
dewacorp.alliances
What you mean by subview? I think you have details view for Tournament then just as part of those details you display PlayRoundHoles just like other Tournament fields. Maybe below other fields.
Trickster
PlayRoundHoles is not field. It's class. The view between the Tournament class and PlayRoundHoles class are different. One is Detail View (Tournament) and the other one is a List View (PlayRoundHoles) on the same page.
dewacorp.alliances
ok it looks like David's in his answer shows what i mean.
Trickster
+1  A: 

Create a ViewModel that contains all the required content for this page. In this case a ViewModel with a Tournament and List of PlayRoundHoles

public class MyViewModel
{
    public Tournament MyTournament { get; set; }
    public IList<PlayRoundHoles> MyPlayRoundHoles { get; set; }
}

Then your action method should return this strongly-viewed type.

public class TournamentController
{
    public ActionResult View(int tournamentId)
    {
        var t = //get tournament
        var p = //call sproc (may use the tournament id)

        MyViewModel model = new MyViewModel();
        model.MyTournament = t;
        model.MyPlayRoundHoles = p;

        return View(model);
    }
}

Your View can then look something like

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MyViewModel>" %>

//tournament details using Model.MyTournament
//play round holes details using Model.MyPlayRoundHoles

To improve on this you could create a PartialView that seperates the display of your PlayRoundHoles

<% Html.RenderPartial("MyPlayRoundHoles", Model.MyPlayRoundHoles); %>
David Liddle
@David: In regard to Model, I have already Linq to SQL and does this mean I have to create separate class called "MyViewModel" like above? Cause in my original Detail view page for Tournament i have: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Golfsys.Model.Tournament>" %> which the model is using Model.Tournament only correct?
dewacorp.alliances
If you already have a Tournament object from Linq2Sql then just use that instead of MyViewModel.
David Liddle