チェックボックスを使用してデータベースを更新しようとしています。データを削除すると、コードが機能します。しかし、データベースに値を追加するために更新すると、エラーが発生しました。
** IDENTITY_INSERT が OFF に設定されている場合、テーブル 'HotelRoomFacility' の ID 列に明示的な値を挿入できません。**
これは私のコード
私のサービス
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>
}
これがデータベース図です