angel
angel

Reputation: 4632

Good MVC practices - when to use partial views

I want to do a module for sales.

I would like to know if using many partial views is good practice or not.

For example:

      <div id="mypage">

      <div id="select_account">

      @*
here the code for select an account or call a partial view?
*@
      </div>

<div id="select_product">

      </div>

<div id="mydetails">

      @*
what is better? use the code for details here? or added them on a partial view?
*@
      </div>

<div id="showingtotals">

      @*
here its shows the iva, subtotal and total then the same question
partialview? or on this page?
*@
      </div>

      </div>

After says that, for example when I added a product, I have that to do 2 calls first for update details div and second for update totals div , then its going to to slow down my system.

My principal question ... Should I use a lot of partial views? Or is it better use the fewest partial views? On this simple example, there are 4 partial views (for select customer, select product, show details, and show totals) (and maybe other for receive the payment)

Upvotes: 1

Views: 3136

Answers (1)

johnnyRose
johnnyRose

Reputation: 7490

The way I see it, partial views are handy for two different cases.

Mirrored functionality

If you possibly plan on adding a widget from your current program to another page, it's definitely better to follow the DRY principle. Partial views can help avoid duplicate code and enhance maintainability.

Extremely complex views

The other situation I find partial views helpful is in extremely cluttered views with a lot of code. Using partial views in this situation can be beneficial for organization.

An example of this might be a "dashboard" page, with multiple widgets. Containing all the code in one file might be difficult to manage, and the widgets should probably be able to stand alone, so it makes sense to separate these out based on their functionality.

However...

Adding partial views for the sake of having partial views can unnecessarily complicate your app and make it more difficult to maintain. Use your best judgement and exercise moderation while focusing on future maintainability.

Finally, another drawback is that partial views are unable to easily render sections.

Upvotes: 7

Related Questions