novian kristianto
novian kristianto

Reputation: 761

how to checked checkbox using view model

i try to make a simple project, that check the list of checkbox. my database is like this... !enter image description here

i want to check my checkbox when hotel have the facilities...

i have code like this...

my controller

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);
        }

my constuctor class

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

            return x;
        }

my view

@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")'" />

how can i make checkbox is checked ?? help me please

Upvotes: 0

Views: 883

Answers (1)

Lotok
Lotok

Reputation: 4607

You want to store the true/false in your database as a bit. 0 is false and 1 is true.

Then when you have a boolean property in your view model, populated by your database

public bool FacilityXAvailable { get; set; }

On your view you can then just do this

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

That will display a checkbox checked on unchecked depending on Db value.

Upvotes: 1

Related Questions