public ActionResult _OtherAssetUpdateForm(OtherAssetUpdateViewModel model) { var asset = OtherAssetQueries.GetOtherAssetById(model.Id); if (!asset.Name.Equals(model.Name) && OtherAssetQueries.CheckExistOtherAsset(UserQueries.GetCurrentUsername(), model.Name)) { ModelState.AddModelError("CheckExistAsset", "Tài sản này đã tồn tại, vui lòng nhập tên khác"); } if (ModelState.IsValid) { int result = OtherAssetQueries.UpdateOtherAsset(model); if (result > 0) { return(Content("success")); } else { return(Content("failed")); } } else { return(PartialView(model)); } }
public static int UpdateOtherAsset(OtherAssetUpdateViewModel model) { Entities entities = new Entities(); var otherAsset = entities.Assets.Where(x => x.Id == model.Id).FirstOrDefault(); otherAsset.AssetName = model.Name; otherAsset.Value = model.Value.Value; var income = entities.Incomes.Where(x => x.AssetId == model.Id).FirstOrDefault(); income.Value = model.Income.HasValue ? model.Income.Value : 0; income.Name = "Thu nhập từ " + model.Name; entities.Incomes.Attach(income); entities.Entry(income).State = System.Data.Entity.EntityState.Modified; return(entities.SaveChanges()); }
public static OtherAssetUpdateViewModel GetOtherAssetById(int id) { OtherAssetUpdateViewModel viewmodel = new OtherAssetUpdateViewModel(); Entities entities = new Entities(); var otherAsset = entities.Assets.Where(x => x.Id == id).FirstOrDefault(); viewmodel.Id = otherAsset.Id; viewmodel.Name = otherAsset.AssetName; viewmodel.Value = otherAsset.Value; if (otherAsset.Incomes1.Where(x => !x.DisabledDate.HasValue).Any()) { viewmodel.Income = otherAsset.Incomes1.FirstOrDefault().Value; } else { viewmodel.Income = 0; } return(viewmodel); }
public ActionResult _OtherAssetUpdateForm(int id) { OtherAssetUpdateViewModel model = OtherAssetQueries.GetOtherAssetById(id); return(PartialView(model)); }