ajax 和 asp.net-mvc - 我应该使用 PartialView 还是 Json? [英] ajax and asp.net-mvc - should I use PartialView or Json?

查看:29
本文介绍了ajax 和 asp.net-mvc - 我应该使用 PartialView 还是 Json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 asp.net-mvc 站点.我有一个页面,上面有一个数据网格,当我想编辑一行时(通过显示一个弹出表单).我有两个选择:

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. 在初始页面加载时将表单预加载并隐藏在隐藏的 div 中,然后从服务器返回 json,在客户端绑定所有字段和属性,然后显示它.

  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.

从 PartialView() 的服务器返回 PartialView() 在这种情况下所有绑定都在服务器端

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.

选项 #2 更容易,因为我可以在 C# 中完成所有绑定(和任何逻辑),但似乎 #1 会快得多,因为我没有通过网络发送所有的 HTML(只是 json数据).这假设客户端的绑定将比额外网络影响的性能影响更快.

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.

在使用 asp.net-mvc 填充表单时,在返回 json 或 PartialView() 的决定中,我是否遗漏了任何其他因素

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

推荐答案

我认为这不是 JSon 或 Partial View 的问题.相反,这是一个方法问题.我会将其重新表述为我应该在隐藏的 div 中加载页面上的数据还是在需要时加载部分视图"?

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.

这篇关于ajax 和 asp.net-mvc - 我应该使用 PartialView 还是 Json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆