RedirectToAction到多个HttpPost操作

时间:2019-03-14 02:26:07

标签: c# asp.net-mvc model-view-controller routes redirecttoaction

我创建了一个按钮,该按钮可以转到带有3个标签的另一页。

这3个子选项卡具有不同的输入文本,并且仅使用1个Action [HttpGet]。

这些是我的情况。

  1. 我只想保存第一个标签数据,2个标签值将为空
  2. 我只想保存第二个数据,第一个标签和第三个标签将为空
  3. 我要保存在第一个标签,第二个标签和第三个标签下输入的所有文本输入。

这是我的代码:

 [HttpGet]
 public ActionResult Create_Data(int productId)
 {
      var model = BigViewModel();
      ///rest of code
      return View(model)
 }

 [HttpPost]
 public ActionResult Create_Data(BigViewModel model, int productId)
 {
      int experimentForOverloading = 0;
      string experimentAgain = ""

      // validates if first tab and doesn't have data inputted, will redirect to Create_SecondTab. Below is just for testing
      if (model.FirstTabName == null && model.ThirdTabDirectory == null)
      {

           // this is where I want to go to route the new Action. But I don't know what to do..
           return RedirectToAction("CreateData", new
           {
                model = BigViewModel,
                Id = productId,
                experimentForOverloading
           }
      }
      else if (model.SecondTabSalary == null && model.ThirdTabDirectory == null)
      {
           return RedirectToAction("CreateData", new
           {
                model = BigViewModel,
                Id = productid
                experimentAgain
           }
      }
      else
      {
           return RandomView(); //Testing purposes
      }
 }

 // This is the second case, save only when first tab is empty
 [HttpPost]
 public ActionResult CreateData(BigViewModel, int bigId, int experimentForOverloading)
 {
      if(ModelState.IsValid)
      {
           //.. code here
           _context.SaveChanges()
      }
      else
      {
           return RandomView(); //Testing purpose
      }
 }

 // This is the first case, save only when second and third tab is empty
 [HttpPost]
 public ActionResult CreateData(BigViewModel, int bigId, string experimentAgain)
 {
      if(ModelState.IsValid)
      {
           //.. code here
           _context.SaveChanges()
      }
      else
      {
           return RandomView(); //Testing purpose
      }
 }

0 个答案:

没有答案