0

I'm still beginning in Web App development and I have a question to ask to any veteran developers.

In my MVC-App I have a part that will be used several times. This part is a search filter for the items my MVC-App manages.

What I want to do is develop this part once so that it may be used by every parts on my app that needs it. That way, if I need to edit this part, I can do so at the basic instance instead of each places I introduced this features, and the modifications will be carried over in a single shot.

So far I know a bit about Partial Views, and right now I'm exploring Web Forms and how they works.

My question is: what would be the best way to develop such a feature? Should I build a partial view that I render each time I need it, and if so, how may I do this? Or should I concentrate on building a web form? Or is there a better way?

Thank you for your answer.

hsim
  • 2,000
  • 6
  • 33
  • 69

1 Answers1

2

Yes, partial views are the way to go.

To do this, add a view and in the wizard check the checkbox partial view (optionally use a strongly typed view).

After that you can render the partial view like this (example for a strongly typed view to a Product-class):

@{
    Html.RenderPartial("MyProductView", Model.Product);
}

Note: you need to do this in a code block since Html.RenderPartial actually writes to the output immediately.

Alternatively, if you don't want to include this in every view, you could also use RenderAction like this:

Controller

public ActionResult MyProduct()
{
    return PartialView();
}

View

@{
    Html.RenderAction("MyProduct", "ControllerName")
}
Kenneth
  • 28,294
  • 6
  • 61
  • 84
  • Model.Product stands as what in your example? – hsim May 06 '13 at 17:43
  • That would be your strongly typed Model from the View where you want want to render the partial view in. So View with a Model (say Cart) references PartialView with a Model (say Cart.Product). This is just as an example of course, you don't need to have strongly typed views – Kenneth May 06 '13 at 17:45
  • Ok, so I would need that my item contains the object model for my partia view? Can't I have this partial view to be based on a model of it's own, then on submit it gives back the object itself? – hsim May 06 '13 at 17:47
  • If you want to do that you can do a renderaction. I have updated my answer to show you the alternative – Kenneth May 06 '13 at 17:50
  • Thanks, I'm progressing! Is there a way to make this partial view somewhat "Central" part? Like, if I want to have access to this partial views from many places, I would need to build a controller dedicated to this view and call on it each time I need it? – hsim May 06 '13 at 18:05
  • Sure, you can. You just have to add it to your "master". In the shared folder there's a file called _Layout.cshtml. You can render your partial view in there. If it's not there you can check out this link on how this works: http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx (in the basic template this gets added upon creation) – Kenneth May 06 '13 at 20:40