public ProductComplex Get(string id) { var info = new ProductComplex(); info.Product = this._repository.Get(x => x.ProductID == id); var query = ( from u in this._alternative.GetAll() where u.ProductID == id join p in this._repository.GetAll() on u.AlternativeID equals p.ProductID into procuct from p in procuct select new AlternativeViewModel() { SeqNo = u.SeqNo, ProductID = u.ProductID, AlternativeID = u.AlternativeID, AlternativeCount = u.AlternativeCount, Remarks = u.Remarks, AlternativeName = p.ProductName, IsDirty = false } ); info.ChildList = query.ToList(); return(info); }
public ActionResult Edit(ProductComplex info) { ResultModel result = new ResultModel(); try { #region 驗證Model if (!ModelState.IsValid) { throw new Exception(ModelStateErrorClass.FormatToString(ModelState)); } var query = info.ChildList .GroupBy(x => x.AlternativeID) .ToDictionary(x => x.Key, x => x.Count()) .Where(x => x.Value > 1) .ToList(); CDMS.Web.Common.DuplicateValidator validator = new CDMS.Web.Common.DuplicateValidator(query); if (validator.Message.Count > 0) { foreach (var s in validator.Message) { ModelState.AddModelError("AlternativeID", s); } } var query2 = info.ChildList .Where(x => x.AlternativeID == info.Product.ProductID) .ToList() .Count; if (query2 > 0) { ModelState.AddModelError("AlternativeID", "替代品不可與產品相同<br/>"); } if (!ModelState.IsValid) { string message = ModelStateErrorClass.FormatToString(ModelState); result.Status = false; result.Message = message; return(Json(result)); } #endregion // 有修改的資料 foreach (var item in info.ChildList.Where(x => x.IsDirty == true)) { Alternative target = Mapper.Map <Alternative>(item); if (target.SeqNo == 0) { this._AlternativeService.Create(target); } else { this._AlternativeService.Update(target); } } #region 息頁面設定 result.Status = true; result.Url = Url.Action("Index", "Alternative", new { id = info.Product.ProductID }); result.Message = "MessageComplete".ToLocalized(); #endregion } catch (Exception ex) { #region 錯誤時錯誤訊息 result.Status = false; result.Message = ex.Message.ToString(); #endregion } return(Json(result)); }