工作示例JavaScriptResult在asp.net mvc的 [英] Working example for JavaScriptResult in asp.net mvc

查看:97
本文介绍了工作示例JavaScriptResult在asp.net mvc的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以提供asp.net的MVC JavaScriptResult的工作示例。我明白,它返回JavaScript,然后在客户端执行,也是响应的内容类型设置为text / JavaScript的。我需要一些工作示例在行动中看到这个事情。

Can somebody provide a working example of JavaScriptResult in asp.net mvc. I understand that it returns javascript which is then executed on the client side and also that the content type of the response is set to text/javascript. I need some working example to see this thing in action.

推荐答案

JavaScriptResult被认为是一种反模式的Asp.net MVC介绍(关注点完全分离),因为它情侣控制器和视图重新走到一起,使它们可靠的海誓山盟。在纯Asp.net MVC应用程序,其中用户界面是建立在Asp.net MVC和服务器端的服务于这个客户端实现只有它是这样的通知,以避免此功能。

Avoid if possible

JavaScriptResult is considered an anti-pattern that Asp.net MVC introduced (complete separation of concerns), because it couples Controller and View back together to make them dependable on eachother. In a pure Asp.net MVC application where the UI is build on Asp.net MVC and server side serves this client implementation only it is thus advised to avoid this functionality.

这可能是在其他情况下非常有用。我还记得我一直在阅读on Rails的客户与红宝石的东西。

It may be useful in other scenarios. I can remember I've been reading something related to Ruby on Rails clients.

反正。

这是实际的例子是JavaScript的code返回一个Ajax请求,将简单的提供,将立刻在响应没有任何数据操作得到执行某些功能。

An actual example would be to return javascript code to an Ajax request that would simply provide some functionality that will get executed immediately upon response without any data manipulation.

如果你能可能从中受益?嗯认为有thoughout应用程序使用的各种客户端类的大量应用程序。但是,某些页面只使用其中的一小部分(甚至是动态的fracion)。在这种情况下,你将有两种可能性:

Where could you possibly benefit from it? Well think of an application that has huge amounts of various client classes used thoughout the application. But certain pages use only a small fraction (or even a dynamic fracion) of them. In this case you would have two possibilities:


  1. 加载整个客户分类的前期的 - 无论是在单独的文件中一个巨大的单个文件或分散的(这将是确定的,如果意见会用一个小的子集前期已知类别,因为否则这将导致大量的服务器的请求)

  2. 按需加载类在需要的时候的 - 或者,如果需要这些需求甚至可能执行特定的类函数

  1. Load the whole client class tree upfront - either in a huge single file or fragmented in separate files (this would be ok if views would use a small sub set of up-front known classes, because otherwise this would result in lots of server requests)
  2. Load classes on demand when they are needed - or maybe even execute certain class functions on demand when and if they are needed.

在该特定情况下,第二个方案是在网络通信,客户机的存储器资源和处理器负载方面好得多和更高效。

In this particular case, the second scenario would be much better and much more efficient in terms of network traffic, client memory resources and processor load.

这篇关于工作示例JavaScriptResult在asp.net mvc的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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