无法从一个动作重定向到另一个MVC4

时间:2013-12-09 20:59:38

标签: asp.net-mvc-4

我是MVc4的新手并试图将参数从一个Action传递到另一个(在另一个控制器中)

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 });
    }

我收到有关参数字典的错误,但我不知道问题是什么

2 个答案:

答案 0 :(得分:1)

关于模型字典的错误表明你正在传递一个“Apple”并且该行动期待一个“橙色”可以这么说。

实际上你的动作是期望一个类型为“ViewModel.Example”的参数,但你发送一个类型为“ExampleData”的参数

将您的最后一行更改为:

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

答案 1 :(得分:0)

您的test.Request对象是ExampleData类的类型,但您的索引操作需要ViewModel.Rate.RateViewModel实例。
所以使用您的数据填充新的ViewModel.Example对象并将其传递给RedirectToAction方法。
    return RedirectToAction("Index", new {search = Your ViewModel.Rate.RateViewModel instance });
确保重定向操作的参数名称应与索引操作参数名称

相同