繁体   English   中英

无法在AJAX POST调用中检索值。 [使用过的AJAX,MVC ASP.net,.net Framework 4.0]

[英]Unable to retrieve value in AJAX POST call. [Used AJAX, MVC ASP.net, .net framework 4.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我得到null或空白对象。 我想要的是从客户端发送的del值。

您需要将内容类型指定为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) {
            }
      });

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

$.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) {
            }
      });

看到这个简单的例子,然后尝试这种方式

这是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”)并传递给控制器​​。

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

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);
        }

首先,您在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);
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM