我尝试使用复选框更新我的数据库,当我删除数据时,我的代码是可行的。但是当我更新为数据库添加一些值时,我遇到了一些错误。
** 当IDENTITY_INSERT设置为OFF时,无法在表'HotelRoomFacility'中为identity列插入显式值。 **
这是我的代码
我的服务
public void UpdateFacilityInHotel(List<int> FacilityIDs, int RoomTypeID)
{
List<HotelRoomFacility> hotelRoomFacilities = _HotelRoomFacility.AsQueryable().Where(f => f.RoomTypeID == RoomTypeID).ToList();
foreach (int newFacility in FacilityIDs)
{
if (hotelRoomFacilities.Where(h => h.RoomFacilityID == newFacility).Count() == 0)
{
HotelRoomFacility facility = new HotelRoomFacility
{
RoomFacilityID = newFacility,
RoomTypeID = RoomTypeID
};
_HotelRoomFacility.Add(facility);
}
}
foreach (HotelRoomFacility facility in hotelRoomFacilities)
{
if (FacilityIDs.Contains(facility.RoomFacilityID) == false)
_HotelRoomFacility.Delete(facility);
}
_HotelFacilityRepository.CommitChanges();
}
我的控制器
public ActionResult EditRoom(int RoomTypeID)
{
ViewBag.hotel = _hotelService.GetByID(_HotelID).HotelName;
//var roomType = _hotelService.ShowRoomByID(RoomTypeID);
//return View(roomType);
List<EditRoomTypeViewModel> editRoomTypeViewModel = _showRoomByIDService.ShowRoomByID(RoomTypeID);
return View(editRoomTypeViewModel.FirstOrDefault());
}
[HttpPost]
public ActionResult EditRoom(EditRoomTypeViewModel typeRoom, FormCollection forms)
{
List<string> IDs = forms["FacilityIDs"].Split(',').ToList();
List<int> FacilityIDs = new List<int>();
foreach (string ID in IDs)
{
FacilityIDs.Add(Convert.ToInt32(ID));
}
_showRoomByIDService.UpdateFacilityInHotel(FacilityIDs, _HotelID);
return EditRoom(_HotelID);
var x = _hotelService.UpdateRoom(typeRoom.RoomTypeID, typeRoom.RoomTypeName, typeRoom.RoomTypeDescription);
if (x != null)
{
return RedirectToAction("Room");
}
return View(typeRoom);
}
我的观点
@model XNet.Repository.Model.EditRoomTypeViewModel
@{
ViewBag.Title = "addRoom";
}
<h2>Edit 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("Index", "Hotel") '" />
</p>
</fieldset>
}
这是数据库图表