1

我尝试使用 MVC 4 创建一些应用程序。在我的应用程序中,我可以创建新的促销。当我在索引中使用 ViewModel 作为参数时,如何将值从 Create 传递到 Index。

这是我的索引

public ActionResult Index(ViewModel.Rate.RateViewModel search)
        {
            var RoomType = _RoomTypeService.GetRoomTypeName(_HotelID);
            var currency = _CurrencyService.GetAllCurrency();

            XNet.WebUI.Hotel.ViewModel.Rate.RateViewModel vm = new ViewModel.Rate.RateViewModel();

            if (search.Request == null)
            {
                vm.RoomTypeList = ViewModel.DropDown.Builder.RoomTypeBuilder.Build(RoomType, null);
                vm.CurrencyList = ViewModel.DropDown.Builder.CurrencyBuilder.Build(currency, null);
            }
            else
            {
                vm.RoomTypeList = ViewModel.DropDown.Builder.RoomTypeBuilder.Build(RoomType, search.Request.RoomTypeID);
                vm.CurrencyList = ViewModel.DropDown.Builder.CurrencyBuilder.Build(currency, search.Request.Currency);
            }

            vm.Request = search.Request;

            if (search.Request == null)
            {
                vm.Request = new RateRequest();
                vm.Request.CheckInFrom = null;
                vm.Request.CheckInTo = null;
                vm.Request.RoomTypeID = null;
            }
            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", "RoomRate is already created, please use edit instead");
                return New();
            }

            ViewModel.Rate.RateViewModel test = new ViewModel.Rate.RateViewModel();
            test.Request = new RateRequest();
            test.Request.RoomTypeID = Convert.ToInt32(vm.NewRoomRate.RoomTypeName);
            return RedirectToAction("Index", new {search = test.Request });
        }

我尝试这样,但我得到这样的错误

Server Error in '/' Application.
The model item passed into the dictionary is of type '<>f__AnonymousType0`1[XNet.WebUI.Hotel.ViewModel.RateRequest]', but this dictionary requires a model item of type 'XNet.WebUI.Hotel.ViewModel.Rate.RateViewModel'. 

有人可以告诉我,我该怎么做?谢谢

4

1 回答 1

2

根据您收到的错误:

Server Error in '/' Application.
The model item passed into the dictionary is of type '<>f__AnonymousType0`1[XNet.WebUI.Hotel.ViewModel.RateRequest]', but this dictionary requires a model item of type 'XNet.WebUI.Hotel.ViewModel.Rate.RateViewModel'.

您指定 Index 将接收类型参数:ViewModel.Rate.RateViewModel命名搜索。

public ActionResult Index(ViewModel.Rate.RateViewModel search)

ViewModel.Rate.RateViewMode但是您在“新”操作结果中传递了一个类型的对象。

ViewModel.Rate.RateViewModel test = new ViewModel.Rate.RateViewModel();
            test.Request = new RateRequest();
            test.Request.RoomTypeID = Convert.ToInt32(vm.NewRoomRate.RoomTypeName);
            return RedirectToAction("Index", new {search = test.Request });

要解决该错误,您可以像这样编辑控制器:

[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", "RoomRate is already created, please use edit instead");
                return New();
            }

            ViewModel.Rate.RateViewModel test = new ViewModel.Rate.RateViewModel();
            return RedirectToAction("Index", new {search = test });
        }

希望有帮助。

于 2013-12-09T19:58:04.400 回答