2

I have an asp.net-mvc site. I have a page with a grid of data on it and when I want to edit a row (by showing up a popup form). I have 2 options:

  1. Preloading and hiding the form on initial page load in a hidden div and then returning json from the server, binding all of the fields and attributes on the client side and then showing it.

  2. Returning PartialView() from the server of a PartialView() in which case all the binding is on the server side

It seems that this is decision of convinience versus performance.

Option #2 is much easier because I can do all the binding (and any logic) in C# but it seems like #1 would be much faster because I am not sending over all of that HTML over the network (just the json data). This assumes that the binding on the client side is going to be faster than the performance hit of the extra network hit.

Is there any other factors that I am missing in this decision to return json or a PartialView() when using asp.net-mvc to populate a form

rene
  • 41,474
  • 78
  • 114
  • 152
leora
  • 188,729
  • 360
  • 878
  • 1,366
  • 5
    That's the eternal question: Partial HTML vs JSON in AJAX. I don't think that you will get a definite answer to this. Both approaches are valid IMHO. – Darin Dimitrov Apr 17 '11 at 16:11
  • @Darin Dimitrov - i understand that either would work (listed above) but i just wanted to see if there was anything i was missing in terms of deciding which one was better for me in this situation. – leora Apr 17 '11 at 16:17
  • @ooo, I think that you perfectly balanced the pros and cons of the two approaches: it's basically performance vs convenience of development. – Darin Dimitrov Apr 17 '11 at 16:18
  • @Darin Dimitrov - whats the best way to compare how much data comes over with json versus the partial view (in bytes). My concern is that migrate to the Json approach (versus the partial view with compression, etc. .) its hard to tell upfront how big of a savings the json will give me . . – leora Apr 17 '11 at 16:26
  • @ooo, FireBug is a great tool for this job. – Darin Dimitrov Apr 17 '11 at 16:27
  • I use Json when it's small object that does nor require a lot of bindings and it's soooooo 2.0 lol – VinnyG Apr 17 '11 at 21:21
  • @VinnyG - why would small or large objects matter. isn't the question about the size of the rest of the page (not the core data) as that would drive the decision around how much "overhead" is in the partialview() – leora Apr 17 '11 at 21:27
  • what I mean is that if you only have to map 2 or 3 properties it's not that long to do it in Json but if you have 20 fields you have to write more code – VinnyG Apr 17 '11 at 22:28
  • @Darin Dimitrov - i guess the other downside of Json() approach is that the grid page now takes longer to load as i am loading a detail page inside a div on that page (to them use the json for when someone loads that). i can't think of any way around that penalty. – leora Apr 17 '11 at 23:28

3 Answers3

1

Another argument in favour of the json way is platform independence. If you do it the json way, it will be much easier later on to switch platforms, if needed. Partial views tie you tightly to the .NET platform. Json can be served from many different platform solutions, for example nodejs.

zszep
  • 4,450
  • 4
  • 38
  • 58
1

I think it is not a question of JSon or Partial View. Rather this is a question of approach. I would rephrase this as "Should I load the data on page in hidden divs or Should I load the partial view when needed"?

I would prefer the partial view as it doesn't make the initial load bulky and also helps to maintain clear seperation of what is rendered when.

sajoshi
  • 2,733
  • 1
  • 18
  • 22
-1

For your particular scenario, i would recommend the partial view method.

here is a great article that discusses options for modal dialogs in mvc. the jQuery-UI dialog is the clear winner and his example works perfect.

http://www.codeproject.com/KB/ajax/jQuery-Modal-Dialogs.aspx

I also like the idea of loading partial views because u can strongly type them, which is always a good idea.

stephen776
  • 9,134
  • 15
  • 74
  • 123
  • what is the reasoning of your recommendation. i am using jquery-ui dialog in either situation so i don't understand how that plays into it. the link again just gives different comparisons against other dialogs . .that is not my issue. – leora Apr 18 '11 at 15:13
  • I didnt share the link for the comparison of the plugins- just for the demo code for the jquery dialog. He provides a clean, reusable method for loading partials into the modal dialog. – stephen776 Apr 18 '11 at 15:50