1

新しいルームを作成しようとしましたが、roomTypeID は常に 1 を返します。私のコードの何が問題なのですか? 新しいルーム タイプを作成できますが、ルーム タイプ 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;
        }

誰かが私に教えてもらえますか、私の間違いはどこですか?? ありがとう

4

1 に答える 1