阿贾克斯JSON来MVC5控制器:含集合传递对象 [英] Ajax json to MVC5 controller: Passing object containing a collection
问题描述
伙计们,我已经尝试这样的MVC4和MVC5。
Folks, I've attempted this on MVC4 and MVC5.
我需要从客户端用几个属性的对象和收集传递到控制器。
I need to pass an object with a few properties and a collection from the client to the controller.
我定义在C#中的模型:
I've defined the model in C# as:
public enum RequestMode { ReadOnly = 0, Edit = 1}
public class DataRequest
{
public int ProjectId { get; set; }
public RequestMode Mode { get; set; }
public List<PageRequest> PageRequests { get; set; }
public DataRequest()
{
PageRequests = new List<PageRequest>();
}
}
public class PageRequest
{
public int Id { get; set; }
public int PageCurrent { get; set; }
public int RowCountPerPage { get; set; }
}
在MVC控制器被定义为(只是用它来设置一个断点来检查请求的值):
The MVC Controller is defined as (just using it to set a breakpoint to check the request values):
[HttpPost]
public JsonResult Test(DataRequest request)
{
return new JsonResult();
}
该Index.cshtml客户端包含Ajax调用:
The Index.cshtml client contains the ajax call:
<script type="text/javascript">
var RequestMode = { ReadOnly: 0, Edit: 1 };
var dataRequest = { ProjectId: 17, Mode: RequestMode.Edit, PageRequests: new Array() };
var pageRequest = { TableId: 3165, PageCurrent: 4, RowCountPerPage: 30 };
dataRequest.PageRequests.push(pageRequest);
$(document).ready(function () {
$.ajax({
data: dataRequest,
type: 'POST',
cache: false,
url: '/Home/Test',
success: function (data) {
},
fail: function (data) {
}
});
});
</script>
我开始调试,页面加载和我的控制器试验方法断点被击中。
I start debugging, the page loads and my breakpoint in the controller Test method gets hit.
在调试器,请求对象显示为:
方式:编辑
PageRequests:计数= 1
专案编号:17
In the debugger, the request object shows up as: Mode: Edit PageRequests: Count = 1 ProjectId: 17
当我展开PageRequests集合属性,我看到:
{} Mvc5WebTest.Controllers.PageRequest
ID:0
PageCurrent:0
RowsPerPage:0
When I expand the PageRequests collection property, I see: {Mvc5WebTest.Controllers.PageRequest} Id: 0 PageCurrent: 0 RowsPerPage: 0
我期望PageRequest对象与我设置的值来填充(即3165,4,30)
I would expect the PageRequest object to be populated with the values I've set (i.e. 3165, 4, 30)
使用招,我看到完整的DataRequest对象越来越转向成JSON正确的,但它似乎MVC控制器不能把它放回C#对象。
Using Fiddler, I see that the complete DataRequest object is getting turned into Json correctly, but it seems that the MVC controller can't turn it back into the C# object.
作为一种变通方法,我可以修改客户机上的Ajax调用
VAR CDR = JSON.stringify(dataRequest);
As a workaround, I can modify the ajax call on the client to var cdr = JSON.stringify(dataRequest);
$.ajax({
dataType: 'json',
type: 'GET',
data: { jsonRequest: cdr },
...
和然后在控制器
[HttpGet]
public ActionResult Test(string jsonRequest)
{
var request = JsonConvert.DeserializeObject<DataRequest>(jsonRequest);
return new JsonResult();
}
这工作,但我宁愿不传递数据作为一个字符串。
This works, but I'd rather not pass the data as a string.
任何人都可以提供一些线索来是怎么回事,我需要做的就是收集填充什么?
Can anyone shed some light as to what is going on and what I need to do to get the collection populated?
推荐答案
由于您张贴JSON数据,你必须使用 JSON.stringify
和声明的contentType:应用/ JSON的;字符集= UTF-8
Because you're posting JSON data, you have to convert the object to string by using JSON.stringify
and declare the contentType: "application/json; charset=utf-8"
$(document).ready(function () {
$.ajax({
data: JSON.stringify(dataRequest),
type: 'POST',
contentType: "application/json; charset=utf-8",
url: '/Home/Test',
success: function (data) {
},
fail: function (data) {
}
});
我也注意到,在C#code,你定义的 PageRequest
为
public class PageRequest
{
public int Id { get; set; }
public int PageCurrent { get; set; }
public int RowCountPerPage { get; set; }
}
而在JS,
var pageRequest = { TableId: 3165, PageCurrent: 4, RowCountPerPage: 30 };
有是在JS C#和 TABLEID
编号
之间的冲突。你将不得不使用一个或另一个。
There is a conflict between Id
in C# and TableId
in js. You will have to use one or the other.
这篇关于阿贾克斯JSON来MVC5控制器:含集合传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!