简体   繁体   English

如何将Javascript数组传递给视图? (MVC4)

[英]How to pass a Javascript Array to a View ? (MVC4)

I am using the jquery datatables in one of my views and want to generate a report with the result of the filtred data, so to generate the report the simplest solution i could come up with was to generate a pdf file (in a view), anyway, to get the filtred results i use the following method : 我正在其中一个视图中使用jquery数据表,并希望生成具有过滤数据结果的报告,因此要生成报告,我能想到的最简单的解决方案是生成pdf文件(在视图中),无论如何,要获得过滤结果,我使用以下方法:

myDataTableHandle = $('#example1').dataTable(...);
var myFilteredRows = myDataTableHandle._('tr', {"filter":"applied"});

To open a view from a js file through a click i found that i could use something like : 通过单击从js文件打开视图,我发现我可以使用类似以下内容的东西:

function foo(id) {
    var url = '@Url.Action("Details", "Branch", new { id = "__id__" })';
    window.location.href = url.replace('__id__', id);
}

Now i am wondering if there is a way to pass a js Array to an MVC view, in other words myFilteredRows, or of there is another way to pass the filtred result to the controller and then from there call the view? 现在我想知道是否有一种方法可以将js数组传递给MVC视图,换句话说,就是myFilteredRows,或者是否有另一种方法将过滤结果传递给控制器​​,然后从那里调用视图?

I hope my question is clear 我希望我的问题很清楚

Looks to me like you have a gap in understanding how this thing works. 在我看来,您在理解这件事是如何工作上存在空白。 In the js code you wrote (foo(id)) you are not calling the view, you are making a request to the server side, so this is passing through the controller (action method branch is getting called -- put a breakpoint there to check) and then the the action method is calling the view and it gets returned to you on the client side. 在您编写的js代码(foo(id))中,您没有调用视图,您是在向服务器端发出请求,因此这是通过控制器传递的(正在调用action方法分支-在此处放置一个断点)检查),然后该action方法调用该视图,并且该视图将在客户端返回给您。

Also in the function you are passing id parameter to the controller action method, you can same way pass your array as a string, but you will need to update the signature on the controller for that action method. 同样在函数中,您将id参数传递给控制器​​的操作方法,您可以通过相同的方式将数组作为字符串传递,但是您将需要为该操作方法更新控制器上的签名。

public ActionResult Branch(int id, string array)

Then you will need to edit your view to accept this array from the controller, either by adding it to the view bag 然后,您需要编辑视图以从控制器接受此数组,方法是将其添加到视图包中

ViewBag.array = array

Or you can add it to your model, if the branch view is accepting a model. 或者,如果分支视图接受模型,则可以将其添加到模型中。 In the view you will have to define an array in a js block to accept this, something like this (this will probably not work as is, you have to debug the js in browser until you get it right) 在视图中,您将必须在js块中定义一个数组以接受此操作,类似这样的操作(这可能无法正常工作,您必须在浏览器中调试js,直到正确为止)

var array = [@ViewBag.array];

The view is an MVC term for HTML content, and it is someting that we deal with on the server side, your controllers work with the views with the help of MVC framework to pass data from the controller to the view. 视图是HTML内容的MVC术语,这是我们在服务器端处理的事情,您的控制器在MVC框架的帮助下使用视图将数据从控制器传递到视图。 When you talk about client side, speaking of the view now becomes vague. 当您谈论客户端时,现在谈论视图变得模糊。 I hope you understand the point. 希望您理解这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM