0

I am building an intranet web site using MVC3. I have a view in which I want to display 3 tables. All 3 tables data are being fetched from one database only. I have 3 different stored procedures for 3 tables data. I am thinking of using 3 different partial views in index.cshtml. And can use 3 stored procedure to fetch data.

Can someone help me how to set controller index method so that it return view for all 3 partial views? I can use viewbag. But I read articles online and it says that its not a good approach to use viewbag. Currently Index method in controller returns view for only one partial view by using 1 stored procedure.

Thanks in advance.

1 Answers1

0

Create a custom view model.

Model:

public class MyViewModel 
{
    public DataSet Result1 { get; set; }
    public DataSet Result2 { get; set; }
    public DataSet Result3 { get; set; }
}

Controller action:

public ViewResult myAction()
{
    var myViewModel = new MyViewModel({
         Result1 = XXX, // get result 1
         Result2 = XXX, // get result 2
         Result3 = XXX  // get result 3
    });

    return View(myViewModel);
}

View:

@model MyViewModel;

<div id="results1">@Model.Result1.Tables[0].Rows[0]["someValueFromDB"].ToString();</div>
... etc.
Terry
  • 14,099
  • 9
  • 56
  • 84
  • This helps but I am trying to use foreach loop in the view, where it says " foreach statement cannot operate on variables of type 'PagedList.IPagedList' because 'PagedList.IPagedList' does not contain a public definition for 'GetEnumerator'" – user1451999 Jun 12 '12 at 22:54
  • Fixed it. I was using paging on the data rerived from stored prodecure. Updated viewmodel class. public class MyViewModel { publicc PagedList.IPagedList results1 {get; set;} //and so on }. I was using PagedList only before. Thanks !! – user1451999 Jun 12 '12 at 23:11