2013-12-09 63 views
2

我是新來MVc4,並試圖從一個操作參數傳遞給另一個(在另一個控制器)廣東話從一個動作重定向到另一個MVC4

public ActionResult Index(ViewModel.Example search) 
      { 

      if (search.Request != null) 
      { 
       Session["RoomTypeID"] = search.Request.RoomTypeID; 
       Session["Breakfast"] = search.Request.Breakfast; 
       Session["Currency"] = search.Request.Currency; 
      } 

      vm.listRoomRate = GetDataIndex(vm.Request); 
      _UserSession.SearchRoomRate = vm; 
      return RedirectToAction("SearchResult"); 
     } 



[HttpPost] 
     public ActionResult New(ViewModel.Rate.RateViewModel vm) 
     { 
     if (vm.NewRoomRate.Currency == null) 
      vm.NewRoomRate.Currency = "IDR"; 

     var NewData = _RoomRateService.NewRoomRate(vm.NewRoomRate.RoomTypeName, vm.NewRoomRate.Breakfast, 
            Convert.ToDateTime(vm.NewRoomRate.CheckInFrom), Convert.ToDateTime(vm.NewRoomRate.CheckInTo), vm.NewRoomRate.sun, vm.NewRoomRate.mon, vm.NewRoomRate.tue, 
            vm.NewRoomRate.wed, vm.NewRoomRate.thu, vm.NewRoomRate.fri, vm.NewRoomRate.sat, vm.NewRoomRate.Currency, vm.NewRoomRate.SingleRate, 
            vm.NewRoomRate.DoubleRate, vm.NewRoomRate.TripleRate, Convert.ToDecimal(vm.NewRoomRate.Commision), Convert.ToInt32(vm.NewRoomRate.Allotment), vm.NewRoomRate.CloseSelling, 
            vm.NewRoomRate.FreeSell); 

     if (NewData == null) 
     { 
      ModelState.AddModelError("failed", "Error has occured"); 
      return New(); 
     } 

     ViewModel.Rate.RateViewModel test = new ViewModel.Example(); 
     test.Request = new ExampleData(); 
     test.Request.RoomTypeID = Convert.ToInt32(v..exampleVM2); 
     return RedirectToAction("Index", new {search = test.Request }); 
    } 

我得到的參數字典的錯誤,但我有不知道問題是什麼

回答

1

關於模型字典的錯誤表明您正在傳遞一個「Apple」,並且動作期待着一個「Orange」可以這麼說。

實際上你的行動期待型「ViewModel.Example」的參數,但要發送類型的參數「ExampleData」

改變你的最後一行是:

return RedirectToAction("Index", new {search = test}); 
0

您的測試.Request對象是ExampleData類的類型,但是您的索引操作期望ViewModel.Rate.RateViewModel實例。
因此使用您的數據填充新的ViewModel.Example對象並將其傳遞給RedirectToAction方法。
return RedirectToAction("Index", new {search = Your ViewModel.Rate.RateViewModel instance });
確保重定向動作的參數名稱應與索引動作參數名稱相同

相關問題