Asp.net mvc 4 如何使用视图模型选中复选框

Asp.net mvc 4 如何使用视图模型选中复选框,asp.net-mvc-4,Asp.net Mvc 4,我试着做一个简单的项目,检查复选框列表。我的数据库是这样的 当酒店有设施时,我想勾选我的复选框 我有这样的代码 我的控制器 public ActionResult Facility() { var model = db.Facilities .Where (htl => htl.FacilityID == hotelFacility.FacilityID)

我试着做一个简单的项目,检查复选框列表。我的数据库是这样的

当酒店有设施时,我想勾选我的复选框

我有这样的代码

我的控制器

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")'" />
@型号列表
@{
ViewBag.Title=“设施”;
}
设施
@使用(Html.BeginForm())
{
身份证件
名称
有空吗
@for(int i=0;ix[i].FacilityID)
@Html.HiddenFor(x=>x[i].FacilityID)
@DisplayFor(x=>x[i].facilityName)
@Html.HiddenFor(x=>x[i].facilityName)
@Html.CheckBoxFor(x=>x[i].facilityAvailable)
}
}

如何使复选框处于选中状态??
请帮助我

您想在数据库中稍微存储真/假。0为假,1为真

然后,当您的视图模型中有一个由数据库填充的布尔属性时

public bool FacilityXAvailable { get; set; }
在你看来,你可以这样做

@Html.DisplayFor(model=>model.FacilityXAvailable)
这将显示一个复选框,该复选框根据Db值选中