0

我有以下实体

实体资产管理

public class AssetMgmt : CommonPropertiesViewModel
{
    public int DepartmentId { get; set; }

    public int TypeOfDevice { get; set; }

    public string DeviceStatus { get; set; }

    public test[] Subscriptions { get; set; }
}

实体测试

public class test
{
    public string IMEINumber { get; set; }

    public int? SubscriptionType { get; set; }

    public string SubscriptionTypeName { get; set; }

    public int CarrierId { get; set; }

    public string CarrierName { get; set; }

    public int AccountId { get; set; }

    public string PhoneNo { get; set; }

    public bool setAsPrimary { get; set; }

    public string SIMNo { get; set; }

    public string Puk1 { get; set; }

    public string Puk2 { get; set; }

    public int Id { get; set; }

    public decimal Maxbudget { get; set; }

    public bool SendConsumption { get; set; }
}

实体 CommonPropertiesViewModel

public class CommonPropertiesViewModel
{

    public int Id { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime? ModifiedOn { get; set; }
    public string CreatedBy { get; set; }
    public string ModifiedBy { get; set; }
    public string CustomMessage { get; set; }
    public int ClientId { get; set; }
}

javascript函数

function SaveNew() {
     var del = { "ClientId" : 0,
        "CreatedBy" : null,
        "CreatedOn" : "/Date(-62135596800000)/",
        "CustomMessage" : null,
        "DepartmentId" : 0,
        "DeviceStatus" : null,
        "Id" : 4,
        "ModifiedBy" : null,
        "ModifiedOn" : null,
        "Subscriptions" : [ { "AccountId" : 421,
              "CarrierId" : 1,
              "CarrierName" : "Airtel",
              "IMEINumber" : "352698040297280",
              "Id" : 8,
              "Maxbudget" : 250.0,
              "PhoneNo" : "9409635039",
              "Puk1" : "2415367892",
              "Puk2" : "9818067434",
              "SIMNo" : "485769351624",
              "SendConsumption" : true,
              "SubscriptionType" : 1,
              "SubscriptionTypeName" : "Voice",
              "setAsPrimary" : true
            },
            { "AccountId" : 421,
              "CarrierId" : 1,
              "CarrierName" : "Airtel",
              "IMEINumber" : "352276053001012",
              "Id" : 9,
              "Maxbudget" : 750.0,
              "PhoneNo" : "4875351547",
              "Puk1" : "9742277228",
              "Puk2" : "2789210574",
              "SIMNo" : "365289701254",
              "SendConsumption" : true,
              "SubscriptionType" : 1,
              "SubscriptionTypeName" : "Voice",
              "setAsPrimary" : true
            }
          ],
        "TypeOfDevice" : 1
      }
      $.ajax({
            type: "POST",
            url: $("#urlprefix").val() + 'AssetManagement/SaveNew',
            data: { "del": JSON.stringify(del) },
            success: function (d) {
                console.log('here');
                return false;
            },
            error: function (msg) {
            }
      });
    }   

完成所有这些之后……我的 AJAX 调用

  [HttpPost]
  public ActionResult SaveNew(AssetMgmt del)
  {
        return View("SaveNew", del);
  }    

当我在调试模式下悬停在在此处输入图像描述突出显示的区域时,我应该得到从客户端发送到服务器的值。

我的问题是,当我进行 AJAX 调用时,del 没有任何价值。服务器端没有收到任何值。SaveNew(AssetMgmt del)中的 IE我得到空对象或空白对象。我想要的是从客户端发送的del值。

4

5 回答 5

4

您需要将内容类型指定为 JSON

$.ajax({
            type: "POST",
            url: $("#urlprefix").val() + 'AssetManagement/SaveNew',
            data: JSON.stringify({ del: del }),
            contentType: 'application/json',
            success: function (d) {
                console.log('here');
                return false;
            },
            error: function (msg) {
            }
      });
于 2013-07-29T07:23:15.353 回答
0

你可以通过这种方式获得价值

$.ajax({
            type: "POST",
            url: $("#urlprefix").val() + 'AssetManagement/SaveNew',
            data: JSON.stringify({ del: del }),
            contentType: 'application/json',
            success: function (d) {
                 alert(d.del);
                console.log('here');
                return false;
            },
            error: function (msg) {
            }
      });
于 2013-07-29T12:32:16.793 回答
0

查看这个简单的示例并尝试这种方式

这是json函数

function SaveNew() {

                var AccountId = 421;

                $.ajax({

                    url: 'SaveNew',
                    type: 'POST',
                    data: JSON.stringify({ AccountId : AccountId  }),
                    dataType: 'json',
                    contentType: 'application/json',
                    success: function (result) {
                        if(result.del==1)
                        {
                           alert('record saved Successfully.');
                        }  
                    },
                    error: function () {
                        alert('Error Occurred');
                    }

                });
            }
        };

这是控制器代码

 [HttpPost]
        public JsonResult SaveNew(int AccountId )
        {
           //here is save code
            var del=1;
            return Json(new { del }, JsonRequestBehavior.AllowGet);
        }

这是完美的工作,现在您可以添加更多字段,例如“AccountId”并传递给控制器​​。

于 2013-07-29T13:15:40.470 回答
0

首先在所有模型类的模型中添加列表

public class AdminList
{
        public List<AssetMgmt> ObjAssetMgmtList { get; set; }
        public List<test> ObjtestList { get; set; }
        public List<CommonPropertiesViewModel> ObjCommonPropertiesList { get; set; }

}

然后

[HttpPost]
        public JsonResult SaveNew(int AccountId )
        {
           AdminList admins1 = new AdminList();

           //add list in admins1 whatever u want then return

            return Json(new { admins1 }, JsonRequestBehavior.AllowGet);
        }
于 2013-07-29T13:30:13.290 回答
0

首先你在javascript函数中使用数组,比如

function SaveNew() {

        var AccountId;
        var CarrierId;
        var CarrierName;

        var del= [];

                del[0] = {
                   AccountId : 421,
                   CarrierId : 1,
                    CarrierName: "Airtel"

                 };
        }
        $.ajax({

                    url: 'SaveNew',
                    type: 'POST',
                    data: JSON.stringify({ del: del}),
                    dataType: 'json',
                    contentType: 'application/json',
                    success: function (result) {
                        //Now u get all field value return back that add in del

                       int id=result.del.AccountId; 

                    },
                    error: function () {
                        alert('Error Occurred');
                    }

                });
            }
        };

然后添加控制器

[HttpPost]
        public JsonResult SaveNew(AdminList[] del)
        {
           AdminList admins1 = new AdminList();

           //here u can get field that added in del like

            int id=del[0].AccountId;

            return Json(new { admins1 }, JsonRequestBehavior.AllowGet);
        }
于 2013-07-30T06:32:17.967 回答