简体   繁体   中英

Cant pass data from one method to another in same controller

I am developing MVC application.

I am trying to pass the data from one method to another method in same controller.

But data doesn't pass properly...

Please check below code... I am trying to pass the Product list from Create to Save data method.

namespace StockWatchScreen.Controllers
{
    public class OrderController : Controller
    {
        public class OrderProduct
        {
            public string SectionCode { get; set; }
            public double Size { get; set; }
            public double Thickness { get; set; }
            public double Length { get; set; }
            public double Quantity { get; set; }   
        }

         public ActionResult Create()
         {
             List<OrderProduct> oProductList = new List<OrderProduct>();

             OrderProduct oProduct = new OrderProduct();
             oProduct.SectionCode = "123";
             oProduct.Length = "123";
             oProduct.Size = "123";
             oProduct.Thickness =  "123";
             oProduct.Quantity = "123";
             oProductList.Add(oProduct);     
            }    

          return RedirectToAction("SaveData", oProductList);           
       }

        public ActionResult SaveData(List<OrderProduct> oProductList)
        {
            ViewBag.ProductList = oProductList;
            ViewBag.OrderNo = "12321#";
            return View();
        }

        }
    }
}

In SaveData method, oProductList list shows always null.

What is reason ?

You need to return : return SaveData( oProductList); .You don't need to return RedirectToAction , and try to avoid using TempData["oProduct"] using TempData in mvc is not good practice. Using AjaxBeginForm you on succes you can get result return SaveData( oProductList); and put it where you want .also you can use UpdateTargetId.

You can't send model like this in RedirectToAction , you should use tempdata for this communicating between actions like this

      public ActionResult Create()
      {
             List<OrderProduct> oProductList = new List<OrderProduct>();
             OrderProduct oProduct = new OrderProduct();
             oProduct.SectionCode = "123";
             oProduct.Length = "123";
             oProduct.Size = "123";
             oProduct.Thickness =  "123";
             oProduct.Quantity = "123";
             oProductList.Add(oProduct);
           }

         TempData["oProduct"] = oProductList; 
         return RedirectToAction("SaveData");
        }

And in the recieving controller

    public ActionResult SaveData(List<OrderProduct> oProductList)
    {
        ViewBag.ProductList = TempData["oProduct"] as List<OrderProduct> ;
        ViewBag.OrderNo = "12321#";
        return View();
    }

This is because RedirectToAction is doing a 301 redirection, and it is actually the client initiating a Get request to the /SaveData action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM