ajax向后台传递多个对象

jquery 的ajax 向后台传递对象时,可以用转换为JSON的方式,我试验了多次,终于找到了可行的防范,在这里做个记录,以方便不会用此方法的博友们备查:

试验代码如下:

一、同时传递两个对象到后台:
1,前台代码:
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
Areas/SystemManage/
@Html.ActionLink("区域","~/SystemManage/Area")
@MvcHtmlString.Create(ViewBag.conn)




   
   
   


   
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       




2,后台代码:

[HttpPost]
        public JsonResult test(testModel treeModel,AreaEntity area)
        {          
            return Json(treeModel.ToJson());
        }

3,testModel 实体类代码:

public class testModel
    {
        public string id { get; set; }
        public string text { get; set; }
        public string parentId { get; set; }
        public AreaEntity data { get; set; }
    }
4,AreaEntity实体类代码:
 public class AreaEntity 
    {
        public string F_Id { get; set; }
        public string F_ParentId { get; set; }
        public int? F_Layers { get; set; }
        public string F_EnCode { get; set; }
        public string F_FullName { get; set; }
        public string F_SimpleSpelling { get; set; }
        public int? F_SortCode { get; set; }
        public bool? F_DeleteMark { get; set; }
        public bool? F_EnabledMark { get; set; }
        public string F_Description { get; set; }
        public DateTime? F_CreatorTime { get; set; }
        public string F_CreatorUserId { get; set; }
        public DateTime? F_LastModifyTime { get; set; }
        public string F_LastModifyUserId { get; set; }
        public DateTime? F_DeleteTime { get; set; }
        public string F_DeleteUserId { get; set; }
    }

二、只传递一个对象到后台(可以把第二个对象做为第一个对象的参数),只需要把上面的第1、第2替换为如下:

1前台代码:

 @{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
Areas/SystemManage/
@Html.ActionLink("区域","~/SystemManage/Area")
@MvcHtmlString.Create(ViewBag.conn)



    
    
    


    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        



    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    }
    function abc()
    {
        var a = $("#form1").serializeObject(); //将表单序列化为JSON对象  
        var b = $("#form2").serializeObject();
        a["data"] = b;
        $.ajax({
            url: "/Home/test",
            data:JSON.stringify(a),
            type: "post",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                $("#abc").append(data);
            }
        });
    }
 
2,后台代码:
 [HttpPost]
        public JsonResult test(testModel treeModel)
        {
            //AreaEntity area = (AreaEntity)treeModel.data;
           // treeModel.data = area;
            return Json(treeModel.ToJson());
        } 

在这里对这个功能做个记录

你可能感兴趣的:(MVC,ajax,M)