我尝试在我的应用程序中使用MVC 4创建一些应用程序,我可以创建新的促销。当我在Index中使用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'.
有人可以告诉我,我怎么能这样做? 感谢
答案 0 :(得分: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'.
您指定索引将收到类型为ViewModel.Rate.RateViewModel
的名为search的参数。
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 });
}
希望有所帮助。