使用Asp.net mvc和jQuery load()将变量传递给视图

时间:2022-11-30 17:42:43

I'm currently loading a view(ascx) into a div using jQuery load(). I want to pass some variables to the view when loading it though so i'm using $.load(view, data); This does not seem to cause any problems but i have no idea how to access the Json object i'm passing in to the control.

我正在使用jQuery load()将视图(ascx)加载到一个div中。我想在加载视图时传递一些变量,所以我使用$。负载(视图、数据);这似乎不会导致任何问题,但我不知道如何访问传递给控件的Json对象。

Here is the jQuery:

jQuery:

var val = {"Id":"1"};
$("#DynamicForm").empty().load('/controller/view', val);

1 个解决方案

#1


2  

In this case jQuery issues a POST request:

在这种情况下,jQuery会发出一个POST请求:

POST /controller/view HTTP/1.1
...

Id=1

So, you can access the Id parameter as Request.Form["Id"], or just as an action parameter:

因此,您可以按请求访问Id参数。形式["Id"],或仅作为动作参数:

public class Controller...
{
    public ActionResult Index(string Id) { ... }
}

#1


2  

In this case jQuery issues a POST request:

在这种情况下,jQuery会发出一个POST请求:

POST /controller/view HTTP/1.1
...

Id=1

So, you can access the Id parameter as Request.Form["Id"], or just as an action parameter:

因此,您可以按请求访问Id参数。形式["Id"],或仅作为动作参数:

public class Controller...
{
    public ActionResult Index(string Id) { ... }
}