0

我有一个 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);

            }
4

1 回答 1

0

查看您拥有的内容,就像您正在尝试将 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);
  }
于 2013-07-10T02:28:45.270 回答