[英]Passing parameters to MVC controller via JavaScript AJAX GET
I am trying to pass a collection of data to my C# controller via AJAX from JavaScript. 我试图通过JavaScript将数据收集通过AJAX传递给C#控制器。 The data being passed will not always be the same.
传递的数据并不总是相同的。 There is no create/update/delete to the system happening at all this is purely a read operation.
完全没有发生对系统的创建/更新/删除操作,这纯粹是读取操作。
My object looks like this: 我的对象看起来像这样:
values = {
Id: [SOME INT ID],
DB: [SOME DB ID],
Values: [{collection of values}]
}
This is my ajax call: 这是我的ajax电话:
$.ajax({
url: "MYURL?" + encodeURIComponent(JSON.stringify(values)),
type: "GET",
success: function(data){
// do callback stuff
},
dataType: "json"
});
My controller is: 我的控制器是:
[HttpGet]
public ActionResult MyController(DataViewModel viewModel){
// Stuff and Things code
}
The data is not being populated in the controller in the viewModel at all. 根本没有在viewModel的控制器中填充数据。 All the values are null.
所有值均为空。 How can I pass the JSON data into the controller?
如何将JSON数据传递到控制器? Thank you in advance.
先感谢您。
Your ajax call should be the following instead: 您的ajax调用应改为以下内容:
$.ajax({
url: "MYURL",
type: "GET",
success: function(data){
// do callback stuff
},
data: values
});
Some issues that existed in your code include that you used a semi-colon instead of a comma at the end of the url:
line and dataType:
sets the response data type (not the request data type, which is sent to the server). 代码中存在的一些问题包括您在
url:
的末尾使用分号而不是逗号url:
line和dataType:
设置响应数据类型(而不是发送到服务器的请求数据类型)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.