[英]Pass list of items from $.get() to MVC controller
My controller Action method
looks like the below: 我的
controller Action method
如下所示:
[HttpGet]
[Route("ShowModal")]
public Task<IActionResult> GetDetails(int id, string name, IEnumerable<Employee> employees)
{
//create a model
//Some business logic codes
return PartialView("_Partial.cshtml", model);
}
I need to call the above Action Method from jQuery's $.get()
method on a button click, capture the partial view returned as HTML
, and show it in a Bootstrap popup. 我需要在单击按钮时从
jQuery's $.get()
方法中调用上述Action方法,捕获作为HTML
返回的部分视图,并将其显示在Bootstrap弹出窗口中。
I am not able to pass the IEnumerable<Employee>
from the jQuery
method, it is always null
, whatever I try. 我无法通过
jQuery
方法传递IEnumerable<Employee>
,无论我尝试什么,它始终为null
。
Below is the JS code: 下面是JS代码:
<a class="btn btn-primary" onclick="ShowModal();" data-keyboard="true" data-toggle="modal">ShowModal</a>
<div class="modal fade" id="divShowModalDialog" role="dialog" tabindex="-1">
<div class="modal-body" id="divShowModalBody">
</div>
</div>
function ShowModal()
{
var list = [{ Id: 101, Gender: 'MALE' }, { Id: 102, Gender: 'FEMALE' }];
list = JSON.stringify(list);
var data = { 'id': 999, 'name': 'JAMES', 'employees': list };
$.get('/Area1/Controller1/ShowModal', data)
.done(function (response) {
if (response != undefined) {
$('#divShowModalBody').html(response);
$('#divShowModalDialog').modal(
{
backdrop: 'static',
keyboard: true,
});
}
})
.fail(function (xhr) {
console.log(xhr);
})
}
I get the id
and name
parameter in the Action method, but the list is always empty. 我在Action方法中获得了
id
和name
参数,但是列表始终为空。 I have tried after removing JSON.stringify()
as well, but it doesn't work. 我也尝试过删除
JSON.stringify()
之后,但是它不起作用。 I know I'm missing a trivial thing, please help. 我知道我缺少一件琐碎的事情,请帮忙。
First, you should be using [HttpPost]
on your controller action and not [HttpGet]
, and of course you'll need to use post from jQuery which is using $.post() and that is because 'POST' is the correct - but not the only - HTTP verb to actually post data to the server side. 首先,您应该在控制器操作上使用
[HttpPost]
而不是[HttpGet]
,当然,您将需要使用jQuery的post,后者使用的是$ .post() ,这是因为'POST'是正确的-但不是唯一-实际上将数据发布到服务器端的HTTP动词。
Second, you shouldn't stringify your employees list before you put it in your data
javascript object that you are sending. 其次,在将雇员列表放入要发送的
data
javascript对象之前,不要对它进行字符串化。 so,
list = JSON.stringify(list);
因此,
and just straight away go list = JSON.stringify(list);
马上就走
var data = { 'id': 999, 'name': 'JAMES', 'employees': list };
You also might need to provide the dataType using $.post(url,data,onsucess,dataType)
check documentation in the link above. 您可能还需要使用
$.post(url,data,onsucess,dataType)
检查文档在上面的链接中提供$.post(url,data,onsucess,dataType)
。
Last, on your action method remove IEnumerable<T>
and replace it with a concrete collection type like List<T>
because the JSON serializer will need to know which type of collection to instantiate at binding time. 最后,在您的操作方法上,删除
IEnumerable<T>
并将其替换为诸如List<T>
的具体集合类型,因为JSON序列化程序将需要知道在绑定时实例化哪种集合类型。
Actually you can achieve it without changing it to POST by using $.ajax() 实际上,您可以通过使用$ .ajax()来实现它而无需将其更改为POST
Use a dictionary object instead of IEnumerable in action method 在操作方法中使用字典对象而不是IEnumerable
public ActionResult GetDetails(int id, string name, Dictionary<int,string> employees)
{
And then in the script 然后在脚本中
var list = [{ Id: 101, Gender: 'MALE' }, { Id: 102, Gender: 'FEMALE' }];
var data = { id: 999, name: 'JAMES', employees: list };
debugger;
$.ajax({
url: '/Home/GetDetails',
type: "GET",
data :data,
contentType: "application/json",
dataType: "json"
});
I was replying to your comment but decided it would be easier to demonstrate my point as an answer. 我是在回复您的评论,但决定将我的观点作为答案会更容易。
To answer your question, no I am not sure. 要回答您的问题,不,我不确定。 But thats why I asked you to try it first, it seems logical as you are passing a
list
and not IEnumerable
to your function. 但这就是为什么我要您首先尝试它的原因,因为在传递
list
而不是对函数进行IEnumerable
时似乎合乎逻辑。
Also, depending on what your Employee
class looks like, you should try this: (you need a constructor in your Employee class for this) 另外,根据您的
Employee
类的外观,应尝试以下操作:(为此,您需要在Employee类中使用构造函数 )
List<Employee> list = new List<Employee>();
list.Add(new Employee(101, 'MALE'));
list.Add(new Employee(102, 'FEMALE'));
var data = { 'id': 999, 'name': 'JAMES', 'employees': list };
...
Update 更新资料
I realize why I'm wrong, I kept thinking in C# terms. 我意识到自己为什么做错了,我一直用C#的方式思考。 Json.stringify() returns a json style string (which C# just sees as a string), so your
public Task GetDetails(int id, string name, IEnumerable employees)
should be public Task GetDetails(int id, string name, string employees)
and then in C#, you need to parse the JSON string. Json.stringify()返回一个json样式字符串(C#只是将其视为字符串),因此您的
public Task GetDetails(int id, string name, IEnumerable employees)
应该是public Task GetDetails(int id, string name, string employees)
然后在C#中,您需要解析JSON字符串。 A helpful link: How can I parse JSON with C#? 一个有用的链接: 如何使用C#解析JSON?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.