Ajax jQuery发送类作为POST参数
我想将类作为post ajax参数发送 我的班级:Ajax jQuery发送类作为POST参数,ajax,jquery,c#-4.0,asp.net-mvc-4,Ajax,Jquery,C# 4.0,Asp.net Mvc 4,我想将类作为post ajax参数发送 我的班级: public class PageInput { public string ActionName { get; set; } public string Controller { get; set; } public int CountPage { get; set; } public int CountRecordInPage { get; set; } p
public class PageInput
{
public string ActionName { get; set; }
public string Controller { get; set; }
public int CountPage { get; set; }
public int CountRecordInPage { get; set; }
public KeyValuePair<string, short> No { get; set; }
}
行动:
public ActionResult GetPageNumbers(PageInput pageInput)
{
return PartialView("_PagingNumbers", pageInput);
}
不起作用。但是为什么呢
当actionResult接收到数据时,结果为空!!但是数据在发送之前是正确的。我猜您没有发布到
url
$.ajax({
url: '@Url.Action("GetPageNumbers", "MyClasses")',
data: '@Model.PageInput',
type: 'POST',
success: function (data) {
console.info(data);
}
});
现在它将发布。您需要在JavaScript代码中创建一个对象,并将其发送到:
$.ajax({
url: '@Url.Action("GetPageNumbers", "MyClasses")',
data: {
ActionName: '@Model.ActionName',
Controller: '@Model.Controller',
CountPage: @Model.CountPage,
CountRecordInPage: @Model.CountRecordInPage,
},
success: function (data) {
alert(data);
}
});
您还需要将
[HttpPost]
属性添加到控制器操作中您应该使用以下模式:
$.ajax({
url: '@Url.Action("GetPageNumbers", "MyClasses")',
data: {"PageInput.CountPage": "@Model.PageInput.CountPage", "PageInput.CountRecordInPage":"@Model.PageInput.CountRecordInPage" },
success: function (data) {
alert(data);
}
});
我想这对你有用。在Ajax post中,有必要在Ajax post参数中使用类的名称。默认的
$.Ajax()
类型是'GET'
,因此如果只需要发布数据,则需要将其设置为'post'
。我更改了“type:'post',但不起作用。为什么?空类是postedi需要的信息比它不工作的更多为什么。给我们一些可以处理的东西,错误等错误不会发生。但数据会发送到emptycool,请参阅更新的代码并打开控制台,F12
,然后告诉我它的响应内容。另外,您正在使用哪个浏览器?错误:服务器响应状态为404(未找到)。我正在使用Chrometh。这是一个很好的解决方案。但如何发送属性“否”?属性“No”是一个KeyValuePair.No。问题已更新。
$.ajax({
url: '@Url.Action("GetPageNumbers", "MyClasses")',
data: {"PageInput.CountPage": "@Model.PageInput.CountPage", "PageInput.CountRecordInPage":"@Model.PageInput.CountRecordInPage" },
success: function (data) {
alert(data);
}
});