使用json上传mvc 4数据库中的批量条目到发布数据

时间:2013-11-30 18:25:56

标签: jquery asp.net-mvc json asp.net-mvc-4

我使用json在我的数据库中上传批量条目,我在最后一个表列中遇到问题,即我在该列中获取空条目我已经列出了我的控制器模型和json下面的内容?我在这个代码中做错了什么< / p>

这是我的控制器

public void ESBulkUpload(List<EntityStructure> model)
        {

            long eSCreatorUserId = WebSecurity.GetUserId(User.Identity.Name);


            foreach (var item in model)
            {
                EntityStructure entityStructure = new EntityStructure();
                //public long EntityStructureId { get; set; }

                //public Nullable<long> EntityId { get; set; }
                entityStructure.EntityId = item.EntityId;

                //public Nullable<long> ESCreatorUserId { get; set; }
                entityStructure.ESCreatorUserId = eSCreatorUserId;

                entityStructure.EntityStructureParentId = item.EntityStructureParentId;

                //public string ESSerialNumber { get; set; }// 1, 1.1, 1.1.2.4.5.2 etc  1.4.3.4.5
                entityStructure.ESSerialNumber = item.ESSerialNumber;

                //public string ESTitle { get; set; }//Root title: Superbrain: Science Humanities
                entityStructure.ESTitle = item.ESTitle;

                //public string ESDescription { get; set; }//small description. collaborative editing of wiki possible.
                entityStructure.ESDescription = item.ESDescription;

                //public Nullable<long> EntityStructureTypeEnumId { get; set; }//is department, is section, is a role etc.
                entityStructure.EntityStructureTypeEnumId = item.EntityStructureTypeEnumId;

                //public Nullable<DateTime> whenAdded { get; set; }
                entityStructure.whenAdded = DateTime.UtcNow.ToLocalTime();
                //public Nullable<int> CountChildNode { get; set; }
                entityStructure.CountChildNode = 0;

                //public Nullable<int> CountTags { get; set; }//resources tagged with the serial number of this node
                entityStructure.CountTags = 0;

                //public Nullable<int> CountMembers { get; set; }
                entityStructure.CountMembers = 0;

                //public Nullable<int> CountVacancies { get; set; }
                entityStructure.CountVacancies = 0;

                entityStructure.isStructureTemplate = false;

                db.EntityStructures.Add(entityStructure);
                db.SaveChanges();

                if (item.EntityStructureParentId != 0)
                {
                    EntityStructure findForeignIdOfStructure = db.EntityStructures.Find(item.EntityStructureParentId);
                    var estructure = from m in db.EntityStructures select m;
                    estructure = estructure.Where(s => s.EntityStructureId == entityStructure.EntityStructureId);

                    findForeignIdOfStructure.children = estructure.ToList();
                    //var entityStructureList = db.EntityStructures.ToList();
                    db.Entry(findForeignIdOfStructure).State = EntityState.Modified;
                    db.SaveChanges();

                    addChildCount(item.EntityStructureParentId);
                }
            }
        }

这是批量用户输入的json

$("#eSBulkUpload").click(function () {
                var array = [
                    {
                    "EntityStructureParentId": 1,
                    "EntityId": 1,
                    "ESSerialNumber": "1",
                    "ESTitle": "School",
                    "ESDescription": "Dav",
                    "EntityStructureTypeEnumId": 0,
                    "isActive": false,
                    "isPublished": false,
                    "isStructureTemplate": false,
                    "TemplateNumber": 0
                    },
                    {
                        "EntityStructureParentId": 1,
                        "EntityId": 1,
                        "ESSerialNumber": "1",
                        "ESTitle": "School",
                        "ESDescription": "Dav",
                        "EntityStructureTypeEnumId": 0,
                        "isActive": false,
                        "isPublished": false,
                        "isStructureTemplate": false,
                        "TemplateNumber": 0
                    },
                    {
                        "EntityStructureParentId": 1,
                        "EntityId": 1,
                        "ESSerialNumber": "1",
                        "ESTitle": "School",
                        "ESDescription": "Dav",
                        "EntityStructureTypeEnumId": 0,
                        "isActive": false,
                        "isPublished": false,
                        "isStructureTemplate": false,
                        "TemplateNumber": 0
                    }
                ]

发布数据的ajax

$.ajax({
                    type: 'POST',
                    url: '/Entity/ESBulkUpload',
                    contentType: 'application/json',
                    data: JSON.stringify(array),
                    success: function (result) {
                        alert("User Uploaded...");
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert("Error : " + errorThrown);
                    }
                });
            });

我的模特

public class EntityStructure
    {
        public EntityStructure()
        {
            this.children = new HashSet<EntityStructure>().ToList();
        }

        public long EntityStructureId { get; set; }
        public long EntityStructureParentId { get; set; }
        public long EntityId { get; set; }

        public long ESCreatorUserId { get; set; }

        public string ESSerialNumber { get; set; }// 1, 1.1, 1.1.2.4.5.2 etc  1.4.3.4.5
        public string ESTitle { get; set; }//Root title: Superbrain: Science Humanities
        public string ESDescription { get; set; }//small description. collaborative editing of wiki possible.

        public long EntityStructureTypeEnumId { get; set; }//is department, is section, is a role etc.

        public Nullable<DateTime> whenAdded { get; set; }

        public int CountChildNode { get; set; }
        public int CountTags { get; set; }//resources tagged with the serial number of this node
        public int CountMembers { get; set; }
        public int CountVacancies { get; set; }
        public bool is![enter image description here][1]Active { get; set; }
        public bool isPublished { get; set; }

        //
        public bool isStructureTemplate { get; set; }//true
        public int TemplateNumber { get; set; }
        public virtual List<EntityStructure> children { get; set; }
    }

数据库错误图像

  

[1]:http://i.stack.imgur.com/Yd4Il.jpg

1 个答案:

答案 0 :(得分:1)

您正在以正确的方式完成此任务。但在这里你错了你的控制器

if (item.EntityStructureParentId != 0)
                {
                    EntityStructure findForeignIdOfStructure = db.EntityStructures.Find(item.EntityStructureParentId);
                    var estructure = from m in db.EntityStructures select m;
                    estructure = estructure.Where(s => s.EntityStructureId == entityStructure.EntityStructureId);

                    findForeignIdOfStructure.children = estructure.ToList();
                    //var entityStructureList = db.EntityStructures.ToList();
                    db.Entry(findForeignIdOfStructure).State = EntityState.Modified;
                    db.SaveChanges();

                    addChildCount(item.EntityStructureParentId);
                }

正确的做法是:

if (item.EntityStructureParentId != 0)
            {
                EntityStructure findForeignIdOfStructure = db.EntityStructures.Find(item.EntityStructureParentId);
                EntityStructure findForeignIdOfStructureLast = db.EntityStructures.Find(entityStructure.EntityStructureId);

                findForeignIdOfStructure.children.Add(findForeignIdOfStructureLast);
                //var entityStructureList = db.EntityStructures.ToList();
                db.Entry(findForeignIdOfStructure).State = EntityState.Modified;
                db.SaveChanges();

                addChildCount(entityStructure.EntityStructureId);
            }