1

i try to create some application using MVC 4. in my application, i can create new promo. how can i pass value from Create to Index when i using ViewModel for parameter in Index.

this my 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");
        }

my New

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

i try like this, but i get error like this

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'. 

can some one tell me, how i can do this? thanks

1 Answer 1

2

according to the error you are getting:

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'.

you are specifying that Index will recieve a parameter of type: ViewModel.Rate.RateViewModel named search.

public ActionResult Index(ViewModel.Rate.RateViewModel search)

but you are passing an object of type ViewModel.Rate.RateViewMode in your "NEW" action result.

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

to resolve the error you can edit your controller like this:

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

hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.