如何将值传递给视图模型

时间:2013-07-10 02:03:33

标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我有一个2视图模型,FacilityInRoomViewModel和NewRoomViewModel,如何从控制器中的FacilityInRoomViewModel向服务器传递值到NewRoomViewModel?

这是我的视图模型

namespace XNet.WebUI.Hotel.ViewModel
{
    public class FacilityInRoomViewModel
    {
        public int RoomFacilityID { get; set; }
        public string RoomFacilityName { get; set; }
        public bool RoomFacilityAvailable { get; set; }
    }
    public class ListFacilityInRoom
    {
        public List<FacilityInRoomViewModel> listRoomFacility { get; set; }
    }
}


namespace XNet.WebUI.Hotel.ViewModel
{
    public class NewRoomViewModel
    {
        public int HotelID { get; set; }

        [Required(ErrorMessage = "*")]
        public string RoomTypeName { get; set; }

        [Required(ErrorMessage = "*")]
        public string RoomTypeDescription { get; set; }

        public List<FacilityInRoomViewModel> facilityinRoom { get; set; }
    }

}

我的viewModelBuilder类

namespace XNet.WebUI.Hotel.ViewModel.Builder
{
    public class FacilityInRoomBuilder
    {
        public static ListFacilityInRoom Build(List<RoomFacility> roomFacility)
        {
            ListFacilityInRoom model = new ListFacilityInRoom();
            model.listRoomFacility = new List<FacilityInRoomViewModel>();

            foreach (var roomFacilities in roomFacility)
            {
                FacilityInRoomViewModel t = new FacilityInRoomViewModel
                {
                    RoomFacilityID = roomFacilities.RoomFacilityID,
                    RoomFacilityName = roomFacilities.RoomFacilityName,
                    RoomFacilityAvailable = roomFacilities.IsActive
                };
                model.listRoomFacility.Add(t);
            }
            return model;
        }

        public static NewRoomViewModel Build2(List<FacilityInRoomViewModel> list)
        {
            NewRoomViewModel model = new NewRoomViewModel();
            model.facilityinRoom = list;

            return model;
        }
    }
}

我的服务

public List<RoomFacility> showNewRooms()
        {
            List<RoomFacility> showNewRoomViewModel = _RoomFacilityRepository.AsQueryable().ToList();
                return showNewRoomViewModel;
        }

我的控制器

    public ActionResult NewRoom()
            {
                var showNewRoomViewModel = _RoomTypeService.showNewRooms();
                ListFacilityInRoom model = FacilityInRoomBuilder.Build(showNewRoomViewModel);
return View(model);

            }

1 个答案:

答案 0 :(得分:0)

看看你有什么它lloks就像你试图将FacilityInRoomViewModel传递给

public static NewRoomViewModel Build2(List<FacilityInRoomViewModel> list)
    {
        NewRoomViewModel model = new NewRoomViewModel();
        model.facilityinRoom = list;

        return model;
    }

但在NewRoomViewModel中,您期望只有List

的对象

我想你的

 public ActionResult NewRoom()
 {
     var showNewRoomViewModel = RoomTypeService.showNewRooms();
     ListFacilityInRoom model = FacilityInRoomBuilder.Build(showNewRoomViewModel);
     var newroomviewmodel=FacilityInRoomBuilder.Build2(showNewRoomViewModel.listRoomFacility);
     return View(newroomviewmodel);
  }