0

チェックボックスのリストをチェックする簡単なプロジェクトを作成しようとしています。私のデータベースはこんな感じです... !ここに画像の説明を入力

ホテルに設備があるときにチェックボックスをオンにしたい...

私はこのようなコードを持っています...

私のコントローラー

public ActionResult Facility()
        {
            var model = db.Facilities
                        .Where (htl => htl.FacilityID == hotelFacility.FacilityID)
                        .Select(htl => new CheckFacilityVM
                        {
                            FacilityID = htl.FacilityID,
                            facilityName = htl.FacilityName,
                            facilityAvailable = htl.IsActive == true,
                        })
                        .ToList();



            return View(model);
        }

私のコンストラクタークラス

 public Facility ShowRoomFacility(int HotelID)
        {
            var x = (from d in db.Facilities
                     where d.FacilityID == HotelID
                     select d).FirstOrDefault();

            return x;
        }

私の見解

@model List<XNet.WebUI.Hotel.ViewModel.CheckFacilityVM>

@{
    ViewBag.Title = "Facility";
}

<h2>Facility</h2>

@using (Html.BeginForm())
{
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th> is available</th>
            </tr>
        </thead>
        <tbody>
            @for (int i = 0; i < Model.Count; i++)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(x => x[i].FacilityID)
                        @Html.HiddenFor(x => x[i].FacilityID)
                    </td>
                    <td>
                        @Html.DisplayFor(x => x[i].facilityName)
                        @Html.HiddenFor(x => x[i].facilityName)
                    </td>
                    <td>
                        @Html.CheckBoxFor(x => x[i].facilityAvailable)
                    </td>
                </tr>
            }
        </tbody>
    </table>       
}

    <br />
    <input style="width:100px;" type="button" title="Save" value="Save" onclick="location.href='@Url.Action("Index","Hotel")'" />
    <input style="width:100px;" type="button" title="Reset" value="Reset" onclick="location.href='@Url.Action("Facility","Hotel")'" />
    <input style="width:100px;" type="button" title="Cancel" value="Cancel"  onclick="location.href='@Url.Action("Room","Hotel")'" />

チェックボックスをオンにするにはどうすればよいですか?? お願い助けて

4

1 に答える 1

1

true/false をデータベースにビットとして保存します。0 は偽、1 は真です。

次に、ビューモデルにブール値のプロパティがあり、データベースによって設定されている場合

public bool FacilityXAvailable { get; set; }

あなたの見解では、これを行うことができます

@Html.DisplayFor(model=>model.FacilityXAvailable)

これにより、Db 値に応じて、チェックされているチェックボックスが表示されます。

于 2013-05-24T07:25:18.417 に答える