0

working on a small website to get familiar with MS MVC framework and I have a partial view "Browse" that i render in a view "Index" using this:

@Html.Partial("Browse")

the problem here is that the partial "Browse" view requires a collection of objects as model which initialized in the "Browse" function of a Controller called "Products", so unless i make a call to the Browse method of the controller, my view will always have a null collection.

is it possible to initialize the collection before rendering the partial view?

joe
  • 546
  • 1
  • 6
  • 20

1 Answers1

1

There are a couple of options, choosing which one, really depends on how you want the "Browse" functionality to be reused.

Firstly, passing the "Browse" collection model from your Index model (probably the more preferable option)

@Html.Partial("Browse", model.BrowsePartialModel)

Second option, using a ChildAction

[ChildActionOnly]
public ActionResult Browse()
{
    // instatiate BrowseViewModel with appropriate route values etc
    var model = new BrowseViewModel();
    return View(model);
}

Then from your Index view

@Html.Action("Browse")
Brent Mannering
  • 2,316
  • 20
  • 19
  • think i'll stick with the first option, viewmodels are my next goal once i understand basic stuff. thanks – joe Mar 16 '14 at 21:59