开发者

ajax and asp.net-mvc - should I use PartialView or Json?

开发者 https://www.devze.com 2023-02-25 23:29 出处:网络
I have an asp.net-mvc site.I have a page with a grid of data on it and whenI want to edit a row (by showing up a popup form).I have 2 options:

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开发者_StackOverflow 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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消