0

I have a View (Products.cshtml), a Partial View (FeaturedProducts.cshtml), two Controllers (ProductsController and FeaturedProductsController), two models (Products and FeaturedProducts).

I am rendering Partial View (FeaturedProducts.cshtml) inside View (Products.cshtml). Index action of FeaturedProductsController talks to DB and brings a list back and populates controls in FeaturedProducts.cshtml. I want Index action of Partial View Controller (FeaturedProductsController) to be invoked everytime its rendered.

This is what i am doing in View (Products.cshtml) but it doesnt invoke Index action of FeaturedProductsController. Am i missing something here?

@Html.Partial("FeaturedProducts") 
Asdfg
  • 11,362
  • 24
  • 98
  • 175

2 Answers2

0

If you are looking for model data to put in your partial view you need to do that in your ProductsController index action. @Html.Partial("FeaturedProducts") is just going to render the partial view, if you are looking to go back to the controller look into rendering your partial view with an AJAX call.

This should help: asp.net MVC partial view controller action

Community
  • 1
  • 1
esastincy
  • 1,607
  • 8
  • 28
  • 38
  • I dont want my partial view to be dependent on any controller other than its own (FeaturedProductsController). I want it to be independent of where it gets rendered. It has its functionality which doesnt depend on who uses it / renders it. All it should know is if it gets rendered, it should do what is defined in its controller and not what consumer asks. – Asdfg Jun 21 '11 at 15:11
  • So you are looking for it to almost bounce back in forth, from controller to view to partialviewcontrolleraction to partial view? Is that the idea behind what you are looking for? – esastincy Jun 21 '11 at 15:42
  • yeah. kind of. From Controller to View to PartialView to ParitalViewController. – Asdfg Jun 21 '11 at 16:15
  • I think what you are looking for is an ajax call when the partial view is rendered. Think about where this would leave you: From Controller to View to PartialView to ParitalViewController. You would be at the controller, not at a view – esastincy Jun 21 '11 at 16:53
0

Although controllers are aware of the views, the views (full or partial) have no knowledge of the controller that calls them. It's a one way street.

Controllers have the responsibility of passing the information (model or viewModel) to the views. When a "parent view" renders a partial view the "parent view" should pass to the partial whatever information the partial view is expecting.

In your case your might need to fetch in your products controller the information that will be needed to render both the products view and the (partial) featured products view.

Hector Correa
  • 26,290
  • 8
  • 57
  • 73