我尝试创建新房间,但 roomTypeID 总是返回 1,我的代码有什么问题?我可以创建一个新的房间类型,但我不能在我的数据库中插入房间设施,因为 RoomType ID 总是返回 1
这是我的代码..
我的控制器
public ActionResult NewRoom()
{
ViewBag.hotel = _hotelService.GetByID(_HotelID).HotelName;
List<ShowEditRoomViewModel> showEditRoomViewModel = _roomTypeService.showNewRooms();
return View(showEditRoomViewModel.FirstOrDefault());
}
[HttpPost]
public ActionResult NewRoom(FormCollection typeRoom)
{
_roomTypeService.NewRoom(_HotelID, typeRoom["RoomTypeName"], typeRoom["RoomTypeDescription"]);
List<string> IDs = typeRoom["FacilityIDs"].Split(',').ToList();
List<int> FacilityIDs = new List<int>();
foreach (string ID in IDs)
{
FacilityIDs.Add(Convert.ToInt32(ID));
}
_roomTypeService.UpdateFacilityInRooms(FacilityIDs, Convert.ToInt32(typeRoom["RoomTypeID"]));
return NewRoom();
}
我的服务
public void UpdateFacilityInRooms(List<int> FacilityIDs, int RoomTypeID)
{
List<HotelRoomFacility> hotelRoomFacilities = _HotelRoomFacilityRopository.AsQueryable().Where(f => f.RoomTypeID == RoomTypeID).ToList();
foreach (int newRoomFacility in FacilityIDs)
{
if (hotelRoomFacilities.Where(h => h.RoomFacilityID == newRoomFacility).Count() == 0)
{
HotelRoomFacility facility = new HotelRoomFacility
{
RoomFacilityID = newRoomFacility,
RoomTypeID = RoomTypeID
};
_HotelRoomFacilityRopository.Add(facility);
}
}
_HotelRoomFacilityRopository.CommitChanges();
}
我的视图模型
public class ShowEditRoomViewModel
{
public int RoomTypeID { get; set; }
public string RoomTypeName { get; set; }
public string RoomTypeDescription { get; set; }
public List<FaciliyInRoom> facilityinRoom { get; set; }
}
我的观点
@model XNet.Repository.Model.ShowEditRoomViewModel
@{
ViewBag.Title = "NewRoom";
}
<h2>New Room</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Isikan Data</legend>
<div>
@Html.Label("Hotel Name")
</div>
<div>
@ViewBag.hotel
</div>
<br />
<div>
@Html.HiddenFor(model => model.RoomTypeID)
</div>
<br />
<div>
@Html.Label("Room Type Name")
</div>
<div>
@Html.EditorFor(model => model.RoomTypeName)
@Html.ValidationMessageFor(model => model.RoomTypeName)
</div>
<br />
<div>
@Html.Label("Room Type Description")
</div>
<div>
@Html.TextAreaFor(model => model.RoomTypeDescription)
@Html.ValidationMessageFor(model => model.RoomTypeDescription)
</div>
<br />
<table>
<thead>
<tr>
<th>Facility Name</th>
<th> is available</th>
</tr>
</thead>
<tbody>
@foreach (var facility in Model.facilitiesInRoom)
{
<tr>
<td>
@(facility.RoomFacilityName)
</td>
<td style="text-align:center;">
<input type="checkbox" @(facility.RoomFacilityAvailable ? " checked=checked" : null) name="FacilityIDs" value="@facility.RoomFacilityID" />
</td>
</tr>
}
</tbody>
</table>
<br />
<p>
<input type="submit" value="Save" />
<input style="width:100px;" type="button" title="EditHotelDetail" value="Back to Detail" onclick="location.href='@Url.Action("Room", "Hotel") '" />
</p>
</fieldset>
}
我的方法
public List<ShowEditRoomViewModel> showNewRooms()
{
List<RoomType> roomTypes = (from d in _RoomTypeRepository.All()
select d).ToList();
List<ShowEditRoomViewModel> showEditRoomViewModel = new List<ShowEditRoomViewModel>();
foreach (RoomType roomType in roomTypes)
{
showEditRoomViewModel.Add(new ShowEditRoomViewModel
{
RoomTypeID = roomType.RoomTypeID,
facilitiesInRoom = LoadFacilityInRoom()
});
}
return showEditRoomViewModel;
}
有人可以告诉我,我的错误在哪里?谢谢