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