在asp.net mvc5中,如何处理在发布不同数据中发布不同数据的情况

时间:2019-05-08 09:25:47

标签: c# asp.net-mvc

我的表格很长,可以将数据发布到2或3或4个表中,具体取决于视图中选中的复选框。

我的第一个问题:我收到一条错误消息:“并非所有代码路径都返回值” 我意识到在有未处理的案件时会收到此错误,但是我无法捕获此案件。你能帮忙吗?

我的第二个问题:我可以包装吗 var s_request = new S_REQUEST {//使用vm属性映射模型属性}; 在函数中多次调用?

关于代码的组织方式是否有任何评论?

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Initiate(SViewModel vm){
             string selectedWorkStepAction = Request["initiationSteps"].ToString();

        switch (selectedWorkStepAction)
        {
            case "0":
                //throw error "Please select an work step action before submission!"
                //stop processing. get out of action
                //don't submit form
                return View(vm);
            case "1":
                if (vm.Coordinates == true)
                {
                    if (vm.ShareWithContractors == false)
                    {
                        using (var context = new SatelliteServicesDBContext())
                        {
                            context.Database.Log = Console.Write;
                            using (DbContextTransaction transaction = context.Database.BeginTransaction())
                            {
                                try
                                {
                                    if (ModelState.IsValid)
                                    {

                                        var s_request = new S_REQUEST
                                        {
                                            //map required properties with coordinates properties, keep few properties null, they will be updated later
                                            //will get written again
                                            //version one                                                
                                        };

                                        db.S_REQUEST.Add(s_request);
                                        db.SaveChanges();

                                        var a_form = new A_FORM
                                        {
                                            //map all properties except one keep it null, will be updated later
                                        };
                                        db.A_FORM.Add(a_form);
                                        db.SaveChanges();


                                        var S_Contractor = new S_CONTRACTOR
                                        {
                                            //reads results from ajax function
                                        };
                                        db.S_CONTRACTOR.Add(S_Contractor);
                                        db.SaveChanges();

                                        //call approve method if data is saved successfully

                                        return View("Success");
                                    }
                                    return View(vm);
                                }
                                catch (Exception)
                                {
                                    transaction.Rollback();
                                    ModelState.AddModelError("SaveError", "Unable to save changes."); //TODO make sure it shows in view not YSOD
                                    //return View(vm);
                                }
                                //return View(vm);
                            }
                        }
                    }
                    else
                    {
                        using (var context = new SatelliteServicesDBContext())
                        {
                            context.Database.Log = Console.Write;
                            using (DbContextTransaction transaction = context.Database.BeginTransaction())
                            {
                                try
                                {
                                    if (ModelState.IsValid)
                                    {
                                        var s_request = new S_REQUEST
                                        {
                                            //map required properties with coordinates properties, keep few properties null, they will be updated later 
                                            //version one                                           
                                        };

                                        db.S_REQUEST.Add(s_request);
                                        db.SaveChanges();

                                        var a_form = new A_FORM
                                        {
                                           //map all properties except one keep it null, will be updated later 

                                        };
                                        db.A_FORM.Add(a_form);
                                        db.SaveChanges();
                                        //call approve method if data is saved successfully
                                        return View("Success");
                                    }
                                    return View(vm);
                                }
                                catch (Exception)
                                {
                                    transaction.Rollback();
                                    ModelState.AddModelError("SaveError", "Unable to save changes."); //TODO make sure it shows in view not YSOD
                                                                                                      //return View(vm);
                                }
                            }
                        }
                    }
                }
                else //Coordinates checkbox is not checked
                {
                    if (vm.ShareWithContractors == false)
                    {
                        using (var context = new SatelliteServicesDBContext())
                        {
                            context.Database.Log = Console.Write;
                            using (DbContextTransaction transaction = context.Database.BeginTransaction())
                            {
                                try
                                {
                                    if (ModelState.IsValid)
                                    {

                                        var s_request = new S_REQUEST
                                        {
                                            //only required properties
                                            //version two
                                        };

                                        db.S_REQUEST.Add(s_request);
                                        db.SaveChanges();

                                        var a_form = new A_FORM
                                        {
                                            //map all properties except one keep it null, will be updated later 
                                        };
                                        db.A_FORM.Add(a_form);
                                        db.SaveChanges();

                                        var file = new MAP
                                        {
                                            //reads results from ajax function 1
                                            //map all properties
                                        };
                                        db.MAP.Add(file);
                                        db.SaveChanges();

                                        var S_Contractor = new S_CONTRACTOR
                                        {
                                            //reads results from ajax function 2
                                            //map all properties
                                        };
                                        db.S_CONTRACTOR.Add(S_Contractor);
                                        db.SaveChanges();

                                        //call approve method if data is saved successfully
                                        return View("Success");
                                    }
                                    return View(vm);
                                }
                                catch (Exception)
                                {
                                    transaction.Rollback();
                                    ModelState.AddModelError("SaveError", "Unable to save changes."); //TODO make sure it shows in view not YSOD
                                                                                                      //return View(vm);
                                }
                            }
                        }
                    }
                    else
                    {
                        using (var context = new SatelliteServicesDBContext())
                        {
                            context.Database.Log = Console.Write;
                            using (DbContextTransaction transaction = context.Database.BeginTransaction())
                            {
                                try
                                {
                                    if (ModelState.IsValid)
                                    {
                                        var s_request = new S_REQUEST
                                        {
                                           //only required properties
                                            //version two
                                        };

                                        db.S_REQUEST.Add(s_request);
                                        db.SaveChanges();

                                        var a_form = new A_FORM
                                        {
                                            //map all properties except one keep it null, will be updated later 

                                        };
                                        db.A_FORM.Add(a_form);
                                        db.SaveChanges();

                                        var file = new MAP
                                        {
                                            //reads results from ajax function
                                        };
                                        db.MAP.Add(file);
                                        db.SaveChanges();

                                        //call approve method if data is saved successfully
                                        return View("Success");
                                    }
                                    return View(vm);
                                }
                                catch (Exception)
                                {
                                    transaction.Rollback();
                                    ModelState.AddModelError("SaveError", "Unable to save changes."); //TODO make sure it shows in view not YSOD
                                                                                                      //return View(vm);
                                }
                            }
                        }
                    }
                }
                break;

            case "2":
                try
                {
                    if (ModelState.IsValid)
                    {

                        var s_request = new S_REQUEST
                        {
                            //only required properties
                            //version two
                        };

                        db.S_REQUEST.Add(s_request);
                        db.SaveChanges();

                        //delegate method should be called if data is saved successfully

                        return View("Success");
                    }
                    return View(vm);
                }
                catch
                {
                    ModelState.AddModelError("SaveError", "Unable to save changes."); 
                    //TODO make sure it shows in view not YSOD
                    //Console.Write("check error:    " + ex);
                    //return View(vm); //should be inside catch, right?
                }
                break;
            case "3":
                //don't save to any table
                //call cancel method
                return RedirectToAction("Services", "Home");
        }
    }

0 个答案:

没有答案