C# asp.net mvc 中 JavaScriptResult 的工作示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1677325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:54:48  来源:igfitidea点击:

Working example for JavaScriptResult in asp.net mvc

c#javascriptjqueryasp.net-mvc

提问by Ravinder Singh

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.

有人可以在 asp.net mvc 中提供 JavaScriptResult 的工作示例吗?我知道它返回 javascript,然后在客户端执行,并且响应的内容类型设置为 text/javascript。我需要一些工作示例才能看到这个东西的实际效果。

采纳答案by Robert Koritnik

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 advisedto avoid this functionality.

JavaScriptResult 被认为是 Asp.net MVC 引入的反模式(完全分离关注点),因为它将控制器和视图重新耦合在一起,使它们相互依赖。在纯 Asp.net MVC 应用程序中,UI 构建在 Asp.net MVC 上,服务器端仅服务于该客户端实现,因此建议避免使用此功能。

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

它可能在其他场景中很有用。我记得我一直在阅读与 Ruby on Rails 客户端相关的内容。

Anyway.

反正。

An example that does make sense

一个有意义的例子

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.

一个实际示例是将 javascript 代码返回到 Ajax 请求,该请求将简单地提供一些功能,这些功能将在响应时立即执行而无需任何数据操作。

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. 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.
  1. 预先加载整个客户端类树- 无论是在一个巨大的单个文件中还是在单独的文件中分段(如果视图使用一小部分预先已知的类,这将是可以的,因为否则这将导致大量服务器请求)
  2. 在需要时按需加载类- 或者甚至可以在需要时按需执行某些类功能。

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.

在这种特殊情况下,在网络流量、客户端内存资源和处理器负载方面,第二种方案会更好、更高效。

回答by griegs

Check out my reply in this post;

查看我在这篇文章中的回复;

MVC how to return instruction to run javascipt method?

MVC如何返回运行javascipt方法的指令?

That will return a partial view to the page. If you want to itterate through a json object then return a json object from your controller and use something like the following;

这将返回页面的部分视图。如果你想遍历一个 json 对象,那么从你的控制器返回一个 json 对象并使用如下所示的内容;

var obj = eval('(' + msg + ')');

msg above is the returned object from your controller;

上面的 msg 是你的控制器返回的对象;

then,

然后,

$.each(obj.Objects, function() { do something with object });

"Objects" above is a property within the returned json object.

上面的“对象”是返回的 json 对象中的一个属性。

So in c#

所以在 C#

public class JsonObject()
{
  List<MyObjectList> Objects{get;set;}
}

Return the above object to the view.

将上述对象返回到视图。

Does this make sense or would you like a working sample?

这是否有意义或您想要一个工作样本?

回答by Korayem

Note:This answer was written in 2011 and looking at it nowadays, it's more of a hack. It's better to load values through AJAX request that hits a JSON endpoint API.

注意:这个答案是在 2011 年写的,现在看看它,它更像是一个黑客。最好通过命中 JSON 端点 API 的 AJAX 请求加载值。

Here's a practical case: I have a GlobalSettingsstatic C# class that contains static Properties of values that are used through the whole system in the ASP.NET MVC backend side of things.

这是一个实际案例:我有一个GlobalSettings静态 C# 类,其中包含值的静态属性,这些值在 ASP.NET MVC 后端的整个系统中使用。

Some of those values need to be shared with JScode. So I created an Actionthat returns JavaScriptResultwhich basically pumps out those values into global JS variables.

其中一些值需要与 JS代码共享。所以我创建了一个返回JavaScriptResultAction,它基本上将这些值输出到全局 JS 变量中。

Note:Change the output cache period to suit your needs

注意:更改输出缓存周期以满足您的需要

[OutputCache(Duration = 999999)]
public virtual JavaScriptResult Global()
{
        var script = $@"
            MaxNotificaitonsToShow = {GlobalSettings.MaxNotificaitonsToShow};
            ItemsPerPage = {GlobalSettings.ItemsPerPage};
        ";
    return JavaScript(script);
}

And then I load the response of this action as a JS file inside all pages through the HTML footer:

然后我通过 HTML 页脚将此操作的响应作为 JS 文件加载到所有页面中:

<script type="text/javascript" src="/JS/Global"></script>

Now I can get the values in any Javascript file:

现在我可以在任何 Javascript 文件中获取值:

if(ItemsPerPage == 25)
{
   alert('it works!');
}