C#后端接收前端json数组
方案一:
/// <summary> /// (APP端)查询购物车选中商品的总价 /// </summary> public class CartTotalPriceRequestDto { /// <summary> /// UserID /// </summary> [Range(1, 1000000, ErrorMessage = "用户ID值非法")] public int UserID { get; set; } /// <summary> /// 购物车选中的商品 /// </summary> public List<CartTotalPriceItemRequestDto> GoodsList { get; set; } }
/// <summary> /// 购物车选中的商品 /// </summary> public class CartTotalPriceItemRequestDto { /// <summary> /// 商品ID /// </summary> public int GoodsID { get; set; } /// <summary> /// 商品名称 用于校验是否可以下单返回 /// </summary> public string GoodsName { get; set; } /// <summary> /// 商品规格ID /// </summary> public int? SkuProductID { get; set; } /// <summary> /// 购买数量 /// </summary> public int Quantity { get; set; } }
============================================================
/// <summary> /// (APP端)查询购物车选中商品的总价,测试 /// </summary> /// <returns></returns> public ActionResult QueryCartTotalPrice(CartTotalPriceRequestDto req) { string resultJson = HttpAjaxHelper.ActionPost(req, "QueryCartTotalPrice", "Cart"); var resultObj = JsonConvert.DeserializeObject<BaseResponse<decimal>>(resultJson); string data = JsonConvert.SerializeObject(resultObj.Result); return Json(JsonAjax.JsonResult("1", "查询成功", data, "")); }
============================================================
//(APP端)查询购物车选中商品的总价
url: "/ShopUsers/QueryCartTotalPrice",
data: {
"UserID": 4088,
"GoodsList": [
{
"SkuProductID": 6358,
"Quantity": 8,
"GoodsName": "贝德玛舒妍洁肤液",
"GoodsID": 1418
},
{
"SkuProductID": 6358,
"Quantity": 8,
"GoodsName": "贝德玛舒妍洁肤液",
"GoodsID": 1418
}
]
},
============================================================
方案二:
============================================================
<script> $(function () { $("#btnOK").click(function () { var QueryCartTotalPriceObj = { "UserID": "4083", "GoodsList": { "GoodsID": "12", "GoodsName": "33", "SkuProductID": "44", "Quantity": "55" } }; var QueryCartTotalPriceJson = JSON.stringify(QueryCartTotalPriceObj); $.ajax({ type: "Post", dataType: "application/json", //traditional: true,//布尔值,规定是否使用参数序列化的传统样式。这样后台数组变量才能接受到值
============================================================
//(APP端)查询购物车选中商品的总价
url: "/ShopUsers/QueryCartTotalPrice",
data: {
"RequestJson": QueryCartTotalPriceJson,
},
============================================================
/// <summary> /// (APP端)查询购物车选中商品的总价,测试 /// </summary> /// <returns></returns> public ActionResult QueryCartTotalPrice(CartTotalPriceRequestDto req) { string requestJson = Request["RequestJson"]; }
多个参数也一样,"RequestJson"算是一个参数,需要多个参数自己加。
============================================================
方案三
前端ajax请求数据
var reregulationJson = JSON.stringify({ "id": "3", "channelsource": "www", "tradedate": "2018-09-01", "status": "1", "opid": "1", "opname": "", "opdate": "2019-03-02" }); var reregulationareadetailJson = JSON.stringify( [{ "id": "1", "areacode": "类型编号", "areaname": "南区", "gj_amounttotal": "123456", "gn_amounttotal": "777", "gj_ticketcount": "999", "gn_ticketcount": "111", "gj_orders": "666", "gn_orders": "999", "clientcount": "333" }] ); var reregulationairlinedetailJson = JSON.stringify( [ { "id": "123", "airlinecode": "航司编码789", "airlinename": "航司名称", "gj_ticketcount": "111", "gn_ticketcount": "222" }, { "id": "123", "airlinecode": "航司编码789", "airlinename": "航司名称", "gj_ticketcount": "999", "gn_ticketcount": "333" } ]); $.ajax({ url: "/api/Regulate/AddRegulateMain", type: "post", dataType: "json", //data: { // reRegulation: 123, // reRegulationAreadetail: 456, // reRegulationAirlinedetail: null //}, data: { reregulation: reregulationJson, reregulationareadetail: reregulationareadetailJson, reregulationairlinedetail: reregulationairlinedetailJson, },
注意:如果后端对象是List,前端的json必须中括号[],不然反序列化会失败!
----------------------------------------------------------------------------------------------
后端接收数据
[HttpPost] public string AddRegulateMain() { string reRegulationJson = HttpContext.Current.Request["reRegulation"]; string reRegulationAreadetailJson = HttpContext.Current.Request["reRegulationAreadetail"]; string reRegulationAirlinedetailJson = HttpContext.Current.Request["reRegulationAirlinedetail"]; Rs_regulation regulationObj = JsonConvert.DeserializeObject<Rs_regulation>(reRegulationJson); List<Rs_regulation_Areadetail> reRegulationAreadetailObj = JsonConvert.DeserializeObject<List<Rs_regulation_Areadetail>>(reRegulationAreadetailJson); List<Rs_regulation_Airlinedetail> reRegulationAirlinedetailObj = JsonConvert.DeserializeObject<List<Rs_regulation_Airlinedetail>>(reRegulationAirlinedetailJson); return "main"; }
方案四:
public class RegulateController : ApiController
/// <summary> /// 调控列表启用选中项 /// </summary> /// <param name="objRequest"></param> /// <returns></returns> public string Regulation_Enable(EnableRequest objRequest) { //var tst = request.idList; return ("成功"); }
public class EnableRequest { public List<int> idList { get; set; } }
/// 编辑调控维护 url: "/api/Regulate/Regulation_Enable", type: "post", dataType: "json", data: { idList: [9,10,11] },