Using partial views to simplify a complex view - a good approach?

Is it correct to use partial views for the SOLE purpose of breaking down a more complicated view into separate chunks and therefore making it more readable?

It probably seems a silly question but the reason I ask is that everything I've read about using partial views involves a piece of the UI being re-used in multiple places. In my case the piece of UI being put into the partial view is only used in one place. It is therefore done solely for readability, but I'm not sure if the performance impact of this may outweight the increased readability. Thoughts?

-------------Problems Reply------------

Whilst this is correct there are more uses.

  • Reusability
  • Ability to pass pack rendered html from your controller so you can append the partial view to the bottom of containers. Great for jQuery async calls
  • Seperation of concerns
  • Gives developers the ability to work on different sections of a page w/out getting in each others way.

Just to name a few.

I think that you shouldn't be worried about performance until it is a real problem, but you should be worried about your code from the first moment. I usually use partial views to simplify the logic or the structure of my views, as you are wanting to do.

Sure, that's an okay use for them. If it keeps the page organized so it's easier to maintain, I think it's fine.

Also, if I'm using caching, I find it easier to have my:

cache "this" do
# render partial
end

I find it easier to read and keep track of things, especially on an overview or dashboard page where there are lots of different parts of the pages that you're including.

I veto any large and complex view in our projects. Instead, we use RenderAction to extract those pieces of code into smaller chunks/parts. I blogged about it here:

http://eduncan911.com/blog/html-renderaction-for-asp-net-mvc-1-0.aspx

Basically, you move that logic and/or parts into Controllers.

Or, if you are just talking html - then yes, breaking down a view into RenderPartials works too.

I tend to be a bit more conservative. I only use a partial view when I know I'll need to reuse the code or I have multiple complex objects in ViewData that need to be displayed.

There's no right or wrong way here but I have worked on projects where there are a ton of partial views to make things "more simple" and I end up spending forever trying to track down where all the partials are (controller/action folder, shared folder, or elsewhere).

One thing about my approach though is if you have even the slightest thought that the view code may be reused down the line as the project changes use a partial. It will save a bunch of time down the road.

Category:asp.net mvc Views:1 Time:2009-11-11
Tags: asp.net mvc

Related post

  • How Do You Use jQuery to Simplify Your MVC Views? 2008-10-13

    I have found jQuery to be a great tool to simplify my MVC Views. For example, instead of including complicated logic to add alternating styles to my tables I just do this... $(document).ready(function() { $("table.details tr:odd").addClass("detailsAl

  • ASP.NET MVC Complex Views 2009-02-19

    I have a question about complex views in ASP.NET MVC. Where can I find good examples or best practice information on complex views and how best to create them in ASP.NET MVC. I know the basics, but peppering the HTML with complex dynamic HTML and/or

  • SQL Server: Doing a DELETE with a complex VIEW 2009-12-28

    I have a complex VIEW and would like to use the DELETE verb against it. Can this be done? Will the DELETE verb only affect the FROM table used in the SELECT statement? I have about 8 joins. Will any of these joined tables be affected? I am using SQL

  • Defining views in mysql from complex queries 2009-12-31

    NOTE - I HAVE PROVIDED MORE DETAILS AND AN EXAMPLE AND USED CODE BLOCKS (THANKS TO THE HELPFUL COMMENTS ON THIS POST) IN THE QUESTION DEFINING VIEWS IN MYSQL FROM COMPLEX QUERIES -- TRY #2 I'M NOT SURE HOW TO DEPRECATE OR TURN OFF THIS QUESTION... PL

  • Difference between MVC 3 Partial Page (Razor) and MVC 3 View Page with Layout (Razor)? 2010-10-13

    In MVC 3 Beta, is there a difference between the templates MVC 3 Partial Page (Razor) and MVC 3 View Page with Layout (Razor) ? I added a partial page (_partialList) to my application. Now when I return only the partial view, it applies the Layout pr

  • SQL complex view for virtually showing data 2010-12-21

    I have a table with the following table. ---------------------------------- Hour Location Stock ---------------------------------- 6 2000 20 9 2000 24 ---------------------------------- So this shows stock against some of the hours in which there is

  • Validating a partial model within a view with a strongly typed view 2011-02-07

    I have a View called LogIn which has a strongly typed view (ViewModelBase). The login View has a partial called LoginForm which also has a strongly typed view (LogInFormModel). How do I display validation errors for the LoginFormModel? As when I clic

  • Multiple forms in a complex view model - how can I bind just one of them? 2011-02-15

    I have a complex view model which contains a collection of other objects which are recursively rendered using editor and display templates. Each of the items in the collection contains a form model that I would like to submit to a controller. Note, t

  • Including partial views when applying the Mode-View-ViewModel design pattern 2011-03-08

    Consider that I have an application that just handles Messages and Users I want my Window to have a common Menu and an area where the current View is displayed. I can only work with either Messages or Users so I cannot work simultaniously with both V

  • Zend Framework: when to use placeholders, partials, and just plain assigning to view properties? 2011-03-20

    I've been reading through the Zend_View documentation, and I'm not quite sure I understand exactly when it's best to use placeholders, partials, and just using view properties. Here's my basic understanding: Placeholders: use mostly when aggregating

  • NullReferenceException in Partial View, but not in "equivalent" View 2011-08-09

    I'm new to MVC 3 / Razor and working on a simple site to learn the basics. To that end, I've created a simple view that renders a DropDownList on a page at /Home/SignedInAs. The logic in my Home Controller is: public class HomeController : Controller

  • Oracle - JOIN two complex Views - optimizer faults 2011-08-15

    I have two pretty complex views. I need to produce report that will join both by some column. Each of views separately returns in less than a minute with complete result set. When I join them optimizer rebuilds query and makes weird things so result

  • Android sectioned listview with complex view 2011-10-03

    I want to use Jeff S. example for sectioned listview with headers. But I want to use more complex item view. Can you suggest me which approach is best to do this. I found two ways Put information in Map<String,String> item = new HashMap<Stri

  • How to render partials from unrelated controllers in one view-almost Amazon style 2011-11-20

    How can we render many partials from different controllers into one view? The local variables of each partial are calculated separately in different and independent controllers. What I really have to make things more specific, is a home layout, and i

  • Container view controllers vs. complex view hierachies 2012-01-03

    This is an open ended question regarding best practices when creating complex view hierarchies in an iOS app. For the purposes of this question, I define a complex view hierarchy as one that integrates several models, with several views. For example,

  • UISplitViewController and complex view hierarchy 2010-04-14

    I'm doing an iPad tech demo and I'm running into a serious technical problem. I have an app concept that leverages UISplitViewController, but NOT as the primary controller for the entire app. The app flow could be described roughly as this: Home scre

  • How to load complex views concurrently 2010-08-10

    I have an Android activity that creates custom views, and it is an expensive process. I tried to use a Handler, but when I do, the progress dialog that shows, spins like once every 5 seconds. So I tried to use an AsyncTask. Now the progressDialog spi

  • Complex view setup in UIVIew or UIViewController? (Design decision) 2011-01-07

    At the moment I setup most of my UI programmatically. There are two ways of arranging the views that belong to a screen (additionally to using nibs): In the view controller, manage all the views (it's a view controller!) Make a custom UIView subclass

  • iOS Overlay View - Masking UITableViewController with a view is partially masking UITableViewController 2012-03-28

    iPhone - 5.1(9B176) I have been encountering inconsistent behaviour in overlaying a mask view on top of a UITableViewController as shown in picture below. Anyone encountered this behaviour, if yes, please suggest any solutions used? Below is the code

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.122 (s). 11 q(s)