novian kristianto
novian kristianto

Reputation: 761

how to bind data to checkbox from viewmodel

I'm trying to make a view model to show a list of checkboxes. A checkbox will be checked when its ID is found in the database. However, my code is generating an error.

CheckFacilityVN

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace XNet.WebUI.Hotel.ViewModel
{
    public class CheckFacilityVM
    {
        public int FacilityID { get; set; }
        public string facilityName { get; set; }
        public List<FacilityAvailable> facilityAvailable { get; set; }
    }

    public class FacilityAvailable
    {
        public bool isCheck { get; set; }
    }
}

My controller

public ActionResult Facility()
        {
            var htl = _hotelService.ShowRoomFacility(2);
            var list = new List<FacilityAvailable>();
            foreach (var x in htl)
            {
                list.Add(new FacilityAvailable { FacilityID = htl.FacilityID, facilityName = htl.FacilityName, isCheck = htl.IsActive });
            }
            return View();
        }

My constructor

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

            return x;
        }

How can I make these checkboxes?

Upvotes: 1

Views: 3369

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

Start by adapting your view model:

public class CheckFacilityVM
{
    public int FacilityID { get; set; }
    public string FacilityName { get; set; }
    public bool IsFacilityAvailable { get; set; }
}

and then use this view model:

public ActionResult Facility()
{
    var model = _hotelService
        .ShowRoomFacility(2)
        .Select(htl => new CheckFacilityVM
        {
            FacilityID = html.FacilityID,
            FacilityName = html.FacilityName,
            IsFacilityAvailable = htl.IsActive,
        })
        .ToList();

    return View(model);
}

and then write a corresponding view:

@model List<CheckFacilityVM>

@using (Html.BeginForm())
{
    <table>
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Is available</th>
            </tr>
        </thead>
        <tbody>
        @for (var 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].IsFacilityAvailable)
                </td>
            </tr>
        }
        </tbody>
    </table>

    <button type="submit">Save</button>
}

and finally:

[HttpPost]
public ActionResult Facility(List<CheckFacilityVM> model)
{
    // process the values from the view model here ...
}

Upvotes: 1

Related Questions