62,074
社区成员
发帖
与我相关
我的任务
分享
function getTestJson() {
var json1 = {
"PPR_TOA": "2016-07-13",
"Remark": "SiteA",
"WMS_WarehouseInvoicing": [
{
"P_SKU": "9780201370010",
"P_Amount": "122"
},
{
"P_SKU": "1111111111113",
"P_Amount": "133"
}
]
};
$.ajax({
type: "POST",
url: "/MyProject/Test1",
data: json1,
success: function (data) {
alert(data);
}
});
var json2 = {
"data": "{ \"PPR_TOA\": \"2016-07-14\", \"Remark\": \"SiteA工地地址\"," +
"\"WMS_WarehouseInvoicing\": [" +
"{\"P_SKU\": \"9998768985135\", \"P_Amount\": \"120\"}," +
"{\"P_SKU\": \"9780201370010\", \"P_Amount\": \"330\"}" +
"]" +
"}"
};
$.ajax({
type: "POST",
url: "/MyProject/Test2",
data: json2,
success: function (data) {
alert(data);
}
});
}
[HttpPost]
public ActionResult Test1(WMS_PPR ppr)
{
var r = pprd.AddDeliveryPPR(ppr);
if (r > 0)
{
return Content("1");
}
return View();
}
[HttpPost]
public ActionResult Test2(string data)
{
WMS_PPR resq = JsonConvert.DeserializeObject<WMS_PPR>(data);
var r = pprd.AddDeliveryPPR(resq);
if (r > 0)
{
return Content("1");
}
return View();
}
public partial class WMS_PPR
{
public WMS_PPR()
{
this.WMS_PPRD = new HashSet<WMS_PPRD>();
this.WMS_TransportInfo = new HashSet<WMS_TransportInfo>();
this.WMS_WarehouseInvoicing = new HashSet<WMS_WarehouseInvoicing>();
}
[Key]
public int PPR_ID { get; set; }
public string PPR_No { get; set; }
public Nullable<System.DateTime> PPR_TOA { get; set; }
public string Remark { get; set; }
public virtual ICollection<WMS_PPRD> WMS_PPRD { get; set; }
public virtual ICollection<WMS_TransportInfo> WMS_TransportInfo { get; set; }
public virtual ICollection<WMS_WarehouseInvoicing> WMS_WarehouseInvoicing { get; set; }
}
public partial class WMS_WarehouseInvoicing
{
[Key]
public int WI_ID { get; set; }
public string P_SKU { get; set; }
public Nullable<decimal> P_Amount { get; set; }
public Nullable<System.DateTime> CreateTime { get; set; }
public string WI_Remark { get; set; }
[ForeignKey("PPR_ID")]
public virtual WMS_PPR WMS_PPR { get; set; }
[ForeignKey("P_ID")]
public virtual WMS_Product WMS_Product { get; set; }
[ForeignKey("W_ID")]
public virtual WMS_WarehouseInfo WMS_WarehouseInfo { get; set; }
public virtual ICollection<WMS_Delivery> WMS_DeliveryInput { get; set; }
public virtual ICollection<WMS_Delivery> WMS_DeliveryOutput { get; set; }
}
public class WMS_WarehouseInvoicing
{
/// <summary>
/// 9780201370010
/// </summary>
public string P_SKU { get; set; }
/// <summary>
/// 122
/// </summary>
public string P_Amount { get; set; }
}
public class Root
{
/// <summary>
/// 2016-07-13
/// </summary>
public DateTime PPR_TOA { get; set; }
/// <summary>
/// SiteA
/// </summary>
public string Remark { get; set; }
/// <summary>
/// WMS_WarehouseInvoicing
/// </summary>
public List<WMS_WarehouseInvoicing> WMS_WarehouseInvoicing { get; set; }
}
var json1 = {
"PPR_TOA": "2016-07-13",
"Remark": "SiteA",
"WMS_WarehouseInvoicing": [
{
"P_SKU": "9780201370010",
"P_Amount": "122"
},
{
"P_SKU": "1111111111113",
"P_Amount": "133"
}
]
};
$.ajax({
type: "POST",
url: "/MyProject/Test1",
data: json1,
success: function (data) {
alert(data);
}
});
后台直接实体类接收,
[HttpPost]
public ActionResult ReplenishmentNotice(WMS_PPR resq)
{
}
现在只是json对象里面 包含了数组,,,
namespace Web_SJWD.Models.DataModel_WMS
{
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class WMS_PPR
{
public WMS_PPR()
{
this.WMS_PPRD = new HashSet<WMS_PPRD>();
this.WMS_TransportInfo = new HashSet<WMS_TransportInfo>();
this.WMS_WarehouseInvoicing = new HashSet<WMS_WarehouseInvoicing>();
}
[Key]
public int PPR_ID { get; set; }
public string PPR_LOL_Address { get; set; }
public Nullable<System.DateTime> PPR_TOA { get; set; }
public Nullable<System.DateTime> PPR_LoadTime { get; set; }
public Nullable<System.DateTime> PPR_UnloadTime { get; set; }
public string Remark { get; set; }
public virtual ICollection<WMS_PPRD> WMS_PPRD { get; set; }
public virtual ICollection<WMS_TransportInfo> WMS_TransportInfo { get; set; }
public virtual ICollection<WMS_WarehouseInvoicing> WMS_WarehouseInvoicing { get; set; }
}
}
namespace Web_SJWD.Models.DataModel_WMS
{
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public partial class WMS_WarehouseInvoicing
{
[Key]
public int WI_ID { get; set; }
public Nullable<int> W_ID { get; set; }
public Nullable<int> PPR_ID { get; set; }
public string PPR_No { get; set; }
public Nullable<int> P_ID { get; set; }
public string P_SKU { get; set; }
public Nullable<decimal> P_Amount { get; set; }
#region NotMapped
[NotMapped]
public WD_Employee Staff { get; set; }
[NotMapped]
public WD_CompanyInfo Company { get; set; }
#endregion
[ForeignKey("PPR_ID")]
public virtual WMS_PPR WMS_PPR { get; set; }
[ForeignKey("P_ID")]
public virtual WMS_Product WMS_Product { get; set; }
[ForeignKey("W_ID")]
public virtual WMS_WarehouseInfo WMS_WarehouseInfo { get; set; }
public virtual ICollection<WMS_Delivery> WMS_DeliveryInput { get; set; }
public virtual ICollection<WMS_Delivery> WMS_DeliveryOutput { get; set; }
}
}
var json1 = {
"PPR_TOA": "2016-07-13",
"Remark": "SiteA",
"WMS_WarehouseInvoicing": [
{
"P_SKU": "9780201370010",
"P_Amount": "122"
},
{
"P_SKU": "1111111111113",
"P_Amount": "133"
}
]
};
[/quote]
区别在于你的json 里面有些字段没有[/quote]
不管 补全不补全字段的问题, 你这样 那为什么 这两个 字段 PPR_TOA,Remark 有值?
namespace Web_SJWD.Models.DataModel_WMS
{
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class WMS_PPR
{
public WMS_PPR()
{
this.WMS_PPRD = new HashSet<WMS_PPRD>();
this.WMS_TransportInfo = new HashSet<WMS_TransportInfo>();
this.WMS_WarehouseInvoicing = new HashSet<WMS_WarehouseInvoicing>();
}
[Key]
public int PPR_ID { get; set; }
public string PPR_LOL_Address { get; set; }
public Nullable<System.DateTime> PPR_TOA { get; set; }
public Nullable<System.DateTime> PPR_LoadTime { get; set; }
public Nullable<System.DateTime> PPR_UnloadTime { get; set; }
public string Remark { get; set; }
public virtual ICollection<WMS_PPRD> WMS_PPRD { get; set; }
public virtual ICollection<WMS_TransportInfo> WMS_TransportInfo { get; set; }
public virtual ICollection<WMS_WarehouseInvoicing> WMS_WarehouseInvoicing { get; set; }
}
}
namespace Web_SJWD.Models.DataModel_WMS
{
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public partial class WMS_WarehouseInvoicing
{
[Key]
public int WI_ID { get; set; }
public Nullable<int> W_ID { get; set; }
public Nullable<int> PPR_ID { get; set; }
public string PPR_No { get; set; }
public Nullable<int> P_ID { get; set; }
public string P_SKU { get; set; }
public Nullable<decimal> P_Amount { get; set; }
#region NotMapped
[NotMapped]
public WD_Employee Staff { get; set; }
[NotMapped]
public WD_CompanyInfo Company { get; set; }
#endregion
[ForeignKey("PPR_ID")]
public virtual WMS_PPR WMS_PPR { get; set; }
[ForeignKey("P_ID")]
public virtual WMS_Product WMS_Product { get; set; }
[ForeignKey("W_ID")]
public virtual WMS_WarehouseInfo WMS_WarehouseInfo { get; set; }
public virtual ICollection<WMS_Delivery> WMS_DeliveryInput { get; set; }
public virtual ICollection<WMS_Delivery> WMS_DeliveryOutput { get; set; }
}
}
var json1 = {
"PPR_TOA": "2016-07-13",
"Remark": "SiteA",
"WMS_WarehouseInvoicing": [
{
"P_SKU": "9780201370010",
"P_Amount": "122"
},
{
"P_SKU": "1111111111113",
"P_Amount": "133"
}
]
};
[/quote]
区别在于你的json 里面有些字段没有
namespace Web_SJWD.Models.DataModel_WMS
{
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class WMS_PPR
{
public WMS_PPR()
{
this.WMS_PPRD = new HashSet<WMS_PPRD>();
this.WMS_TransportInfo = new HashSet<WMS_TransportInfo>();
this.WMS_WarehouseInvoicing = new HashSet<WMS_WarehouseInvoicing>();
}
[Key]
public int PPR_ID { get; set; }
public string PPR_LOL_Address { get; set; }
public Nullable<System.DateTime> PPR_TOA { get; set; }
public Nullable<System.DateTime> PPR_LoadTime { get; set; }
public Nullable<System.DateTime> PPR_UnloadTime { get; set; }
public string Remark { get; set; }
public virtual ICollection<WMS_PPRD> WMS_PPRD { get; set; }
public virtual ICollection<WMS_TransportInfo> WMS_TransportInfo { get; set; }
public virtual ICollection<WMS_WarehouseInvoicing> WMS_WarehouseInvoicing { get; set; }
}
}
namespace Web_SJWD.Models.DataModel_WMS
{
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public partial class WMS_WarehouseInvoicing
{
[Key]
public int WI_ID { get; set; }
public Nullable<int> W_ID { get; set; }
public Nullable<int> PPR_ID { get; set; }
public string PPR_No { get; set; }
public Nullable<int> P_ID { get; set; }
public string P_SKU { get; set; }
public Nullable<decimal> P_Amount { get; set; }
#region NotMapped
[NotMapped]
public WD_Employee Staff { get; set; }
[NotMapped]
public WD_CompanyInfo Company { get; set; }
#endregion
[ForeignKey("PPR_ID")]
public virtual WMS_PPR WMS_PPR { get; set; }
[ForeignKey("P_ID")]
public virtual WMS_Product WMS_Product { get; set; }
[ForeignKey("W_ID")]
public virtual WMS_WarehouseInfo WMS_WarehouseInfo { get; set; }
public virtual ICollection<WMS_Delivery> WMS_DeliveryInput { get; set; }
public virtual ICollection<WMS_Delivery> WMS_DeliveryOutput { get; set; }
}
}
var json1 = {
"PPR_TOA": "2016-07-13",
"Remark": "SiteA",
"WMS_WarehouseInvoicing": [
{
"P_SKU": "9780201370010",
"P_Amount": "122"
},
{
"P_SKU": "1111111111113",
"P_Amount": "133"
}
]
};
$.ajax({
method: "POST",
url: "@ViewBag.DataUrl",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
pageIndex: self.PageIndex(),
pageSize: self.PageSize(),
sortField: self.SortField(),
sortDirection: self.SortDirection(),
parameters:self.SearchModel()
}),
success: function (data) {
if (data.Success) {
self.DataList(data.Data);
self.PageCount(data.PageCount);
self.TotalCount(data.TotalCount);
self.InitPageNav(data.PageCount);
}
else {
alert("获取数据列表失败:" + data.Message);
}
}
});
$.ajax({
method: "POST",
url: "@ViewBag.DataUrl",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
pageIndex: self.PageIndex(),
pageSize: self.PageSize(),
sortField: self.SortField(),
sortDirection: self.SortDirection(),
parameters:self.SearchModel()
}),
success: function (data) {
if (data.Success) {
self.DataList(data.Data);
self.PageCount(data.PageCount);
self.TotalCount(data.TotalCount);
self.InitPageNav(data.PageCount);
}
else {
alert("获取数据列表失败:" + data.Message);
}
}
});
另外 可以了解下JSON.stringify 这个方法