public static JsonResult AjaxSave(MasterDetailInfoViewModel model, List<DetailInfoViewModel> modelList) { JsonResult jsonResult = new JsonResult(); try { if (model.CustomerId != 0 && model.CustomerName != null && model.Address != null) { if (model != null) { if (modelList.Any()) { //Save MasterDetails Data MasterDetailInfoModel masterDetailInfoModel = new MasterDetailInfoModel() { CustomerID = model.CustomerId, CustomerName = model.CustomerName, Address = model.Address, LoadDate = DateTime.Now }; List<DetailInfoModel> detailInfoModelList = new List<DetailInfoModel>(); ////Master Data Insert //if (InsertToMasterDetailInfo(masterDetailInfoModel) > 0) //{ //var lastInsetMasterDetailInfo = _db.tbl_MasterDetailInfo.FirstOrDefault(x => x.CustomerID == model.CustomerId && x.CustomerName == model.CustomerName); ////Details Data Insert //if (lastInsetMasterDetailInfo != null) //{ foreach (var item in modelList) { DetailInfoModel detailInfoModel = new DetailInfoModel() { //CustomerID = model.CustomerId, ItemSizeID = item.ItemSizeId, Quantity = item.Quantity }; //if (InsertToDetailInfo(detailInfoModel) > 0) //{ // continue; //} //else //{ // break; //} detailInfoModelList.Add(detailInfoModel); } //} //} //Save MasterDetails Data if (InsertMasterDetailInfoBySP(masterDetailInfoModel, detailInfoModelList)) { jsonResult.msg = "Data saved successfully to server."; jsonResult.status = MessageType.success.ToString(); return jsonResult; } else { jsonResult.msg = "Data can not saved successfully to server."; jsonResult.status = MessageType.warn.ToString(); return jsonResult; } } else { //Details Data Null Message jsonResult.msg = "Details data could not found."; jsonResult.status = MessageType.warn.ToString(); return jsonResult; } } else { //Master Data Null Message jsonResult.msg = "Master data could not found."; jsonResult.status = MessageType.warn.ToString(); return jsonResult; } } jsonResult.msg = "Data is required"; jsonResult.status = MessageType.info.ToString(); return jsonResult; } catch (Exception ex) { jsonResult.msg = ExceptionHelper.ExceptionMessageFormat(ex); jsonResult.status = MessageType.error.ToString(); return jsonResult; } }
/// <summary> /// Insert Method for Insert tbl_DetailInfo Data /// </summary> /// <param name="model"></param> /// <returns></returns> private static int InsertToDetailInfo(DetailInfoModel model) { try { tbl_MasterDetailInfo masterDetailInfo = _db.tbl_MasterDetailInfo.FirstOrDefault(x => x.CustomerID == model.CustomerID); tbl_ItemSize itemSize = _db.tbl_ItemSize.FirstOrDefault(x => x.ItemSizeID == model.ItemSizeID); tbl_DetailInfo entity = new tbl_DetailInfo() { CustomerID = model.CustomerID, ItemSizeID = model.ItemSizeID, Quantity = model.Quantity, tbl_ItemSize = itemSize, tbl_MasterDetailInfo = masterDetailInfo }; //Add the object to Entity Set _db.AddTotbl_DetailInfo(entity); //Save the Entity Set return _db.SaveChanges(); } catch (Exception ex) { throw ex; } }