public ActionResult Create([Bind(Include = "Schedule_id,Designer_id,Date,Session_id,Reference_order_detail,On_work_flag")] Designer_Schedule_ViewModel work_schedule) { if (ModelState.IsValid) { //db.Work_schedule.Add(work_schedule); //db.SaveChanges(); return(RedirectToAction("Index")); } //ViewBag.Designer_id = new SelectList(db.Designer, "Designer_id", "Designer_id", work_schedule.Designer_id); //ViewBag.Session_id = new SelectList(db.Session, "Session_id", "Start_time", work_schedule.Session_id); return(View(work_schedule)); }
//取所有session //public SelectList GetAllSession() //{ // var result = new SelectList(context.Session, "Session_id", "Start_time"); // //var result = context.Session.Include(s => s.Session_id) // // .Select(x => new Designer_Schedule_ViewModel // // { // // AllSession = x.Session_id // // }).ToList(); // return result; //} //取所有designer_id //public SelectList GetAllDesigner() //{ // var result = new SelectList(context.Designer, "Designer_id", "Name"); // //var result = context.Designer.Include(x => x.Designer_id) // // .Select(x => new Designer_Schedule_ViewModel // // { // // AllDesignerId = x.Designer_id // // }).ToList(); // return result; //} //Create public OperationResult Create(Designer_Schedule_ViewModel input) { OperationResult result = new OperationResult(); try { //為了要叫用Repository這個類別的Create方法,所以要new一個Repository出來 // ,並且傳入<T>泛型參數,這裡用的參數為要新增內容的資料表名稱 AppleLaLaRepository <Work_schedule> repository = new AppleLaLaRepository <Work_schedule>(context); //為了要對Work_Schedule這個資料表塞資料,所以要new一個Work_Schedule資料表出來 Work_schedule entity = new Work_schedule() { //資料表欄位 = ViewModel欄位 Designer_id = input.AllDesignerId, Session_id = input.SessionId, On_work_flag = input.WorkFlag, Date = input.WorkDate }; //叫用repository裡的Create方法,並傳入整理好(符合Work_Schedule這個資料表欄位)的這包資料 repository.Create(entity); //叫dbContext幫我確定儲存好 context.SaveChanges(); //當走完以上的步驟都沒有出錯,就表示資料儲存完畢 //就可以塞個操作成功給result result.IsSuccessful = true; } //有try就一定會有catch去接住例外狀況發生時要做的事情 catch (Exception ex) { //進入例外狀況這裡,就代表操作失敗 //就要塞個操作失敗給result result.IsSuccessful = false; //將這個被抓到的例外狀況的原因塞給exception這個欄位記錄下來 result.exception = ex; } //將Create這個方法的操作結果回傳出去 return(result); }
public ActionResult Create(/*[Bind(Include = "AllDesignerId, WorkDate, WorkFlag, SessionId")] */ Designer_Schedule_ViewModel model) { if (ModelState.IsValid) { //要取得這個Service裡的Create方法的內容結果出來,所以要先new一個Service出來 Designer_Schedule_Service service = new Designer_Schedule_Service(); //叫用Service裡的Create方法並且將參數帶入此方法 var result = service.Create(model); //判斷是否操作成功,並顯示結果於畫面上給使用者 if (result.IsSuccessful) { ViewBag.result = "資料新增成功"; } else { ViewBag.result = result.exception; } } return(View()); }