Example #1
0
        public async Task <ActionResult> EditAttribute(int pid, int aid, string optionType, IFormCollection fc)
        {
            var success = false;
            var message = string.Empty;
            var _id     = -1;

            try
            {
                if (aid < 1)
                {
                    var prop = repo.GetGeneratedProperty().Where(x => x.ID == pid).FirstOrDefault();
                    var att  = new GeneratedAttribute {
                        Type = optionType
                    };
                    var model = ApiHelper.CreateInstance(ApiHelper.GetType(optionType));
                    await TryUpdateModelAsync(model, model.GetType(), "");

                    var value = JsonConvert.SerializeObject(model);
                    att.Value = value;
                    prop.Attributes.Add(att);
                    repo.SaveChanges();
                    _id = att.ID;
                }
                else
                {
                    var att   = repo.GetGeneratedAttribute().Where(x => x.ID == aid).FirstOrDefault();
                    var model = ApiHelper.CreateInstance(ApiHelper.GetType(optionType));
                    await TryUpdateModelAsync(model, model.GetType(), "");

                    var value = JsonConvert.SerializeObject(model);
                    att.Value = value;
                    repo.SaveChanges();
                    _id = att.ID;
                }
            }
            catch (Exception ex) {
                message = ex.Message;
            }

            return(Json(new { success = true, message = message, id = _id }));
        }