Ejemplo n.º 1
0
        public ActionResult AddCompany(AddCompanyModel AddItem)
        {
            MessageModel result = new MessageModel();

            try
            {
                if (string.IsNullOrEmpty(AddItem.Company.name))
                {
                    result.State   = 0;
                    result.Messgae = "公司名不能为空";
                    return(Json(result));
                }
                var tagids   = "";
                var modelids = "";
                //添加model
                for (var i = 0; i < AddItem.Models.Count(); i++)
                {
                    if (i > 0)
                    {
                        modelids += ",";
                    }
                    AddItem.Models[i].typeids = AddItem.Models[i].typeids == null ? "" : AddItem.Models[i].typeids;
                    db.model.Add(AddItem.Models[i]);
                    db.SaveChanges();
                    modelids += AddItem.Models[i].id;
                    var fileArray = string.IsNullOrWhiteSpace(AddItem.Models[i].file) ? new string[0] : AddItem.Models[i].file.Split(';').Where(t => t != "").ToArray();
                    AddItem.Models[i].file = fileArray.Length > 1 ? string.Join(";", fileArray) : (fileArray.Length == 0 ? "" : fileArray[0]);
                    if (AddItem.Models[i].typeids != null)
                    {
                        tagids += (tagids == "" ? "" : ",") + AddItem.Models[i].typeids;
                    }
                }

                var item = db.Company.SingleOrDefault(t => t.name == AddItem.Company.name & t.type == AddItem.Company.type);
                if (item != null)
                {
                    result.State   = 0;
                    result.Messgae = "该公司名已经存在";
                    return(Json(result));
                }
                tagids = ArrayChange.StrsToStr(tagids.Split(',').Distinct().ToArray());
                AddItem.Company.tagids   = tagids;
                AddItem.Company.modelids = modelids;
                var fileArray2 = string.IsNullOrWhiteSpace(AddItem.Company.description) ? new string[0] : AddItem.Company.description.Split(';').Where(t => t != "").ToArray();
                AddItem.Company.description = fileArray2.Length > 1 ? string.Join(";", fileArray2) : (fileArray2.Length == 0 ? "" : fileArray2[0]);
                db.Company.Add(AddItem.Company);
                db.SaveChanges();
                result.State   = 1;
                result.Messgae = "添加成功";
                return(Json(result));
            }
            catch (Exception ex)
            {
                result.State   = 0;
                result.Messgae = ex.ToString();
                return(Json(result));
            }
        }
Ejemplo n.º 2
0
        public ActionResult SaveCompany(AddCompanyModel AddItem)
        {
            MessageModel result = new MessageModel();

            try
            {
                if (string.IsNullOrEmpty(AddItem.Company.name))
                {
                    result.State   = 0;
                    result.Messgae = "公司名不能为空";
                    return(Json(result));
                }

                var item = db.Company.SingleOrDefault(t => t.id == AddItem.Company.id);
                if (item == null)
                {
                    result.State   = 0;
                    result.Messgae = "该公司不存在";
                    return(Json(result));
                }
                //验证公司名是否合法
                var item2 = db.Company.SingleOrDefault(t => t.name == AddItem.Company.name & t.id != AddItem.Company.id);
                if (item2 != null)
                {
                    result.State   = 0;
                    result.Messgae = "该公司名已经存在";
                    return(Json(result));
                }

                //删除之前的model,绑定新的
                if (!string.IsNullOrEmpty(item.modelids))
                {
                    var modelids2 = item.modelids.Split(',');
                    foreach (var modelid in modelids2)
                    {
                        var i_modelid = int.Parse(modelid);
                        var model     = db.model.SingleOrDefault(t => t.id == i_modelid);
                        db.model.Remove(model);
                        db.SaveChanges();
                    }
                }
                //添加model
                var tagids   = "";
                var modelids = "";
                for (var i = 0; i < AddItem.Models.Count(); i++)
                {
                    if (i > 0)
                    {
                        modelids += ",";
                    }
                    var fileArray = string.IsNullOrWhiteSpace(AddItem.Models[i].file) ? new string[0] : AddItem.Models[i].file.Split(';').Where(t => t != "").ToArray();
                    AddItem.Models[i].file    = fileArray.Length > 1 ? string.Join(";", fileArray) : (fileArray.Length == 0 ? "" : fileArray[0]);
                    AddItem.Models[i].typeids = AddItem.Models[i].typeids == null ? "" : AddItem.Models[i].typeids;
                    db.model.Add(AddItem.Models[i]);
                    db.SaveChanges();
                    modelids += AddItem.Models[i].id;
                    if (AddItem.Models[i].typeids != null)
                    {
                        tagids += (tagids == "" ? "" : ",") + AddItem.Models[i].typeids;
                    }
                }
                tagids            = ArrayChange.StrsToStr(tagids.Split(',').Distinct().ToArray());
                item.tagids       = tagids;
                item.modelids     = modelids;
                item.name         = AddItem.Company.name;
                item.type         = AddItem.Company.type;
                item.address      = AddItem.Company.address;
                item.connector    = AddItem.Company.connector;
                item.connectnum   = AddItem.Company.connectnum;
                item.connectorpos = AddItem.Company.connectorpos;
                item.officiaweb   = AddItem.Company.officiaweb;
                item.remark       = AddItem.Company.remark;
                var fileArray2 = string.IsNullOrWhiteSpace(AddItem.Company.description) ? new string[0] : AddItem.Company.description.Split(';').Where(t => t != "").ToArray();
                item.description = fileArray2.Length > 1 ? string.Join(";", fileArray2) : (fileArray2.Length == 0 ? "" : fileArray2[0]);
                db.SaveChanges();
                result.State   = 1;
                result.Messgae = "修改成功";
                return(Json(result));
            }
            catch (Exception ex)
            {
                result.State   = 0;
                result.Messgae = ex.ToString();
                return(Json(result));
            }
        }