Monday, 20 February 2017

MVC - Display all values from table as chechboxes

Watch on YouTube




1. My Stored Procedure

ALTER PROCEDURE GetEmployeeStatus
AS
BEGIN
    SET NOCOUNT ON;
    SELECT        StatusID As EmployeeStatusID, Name As EmployeeStatusName, Cast(0 As Bit) as IsSelected
FROM            EmployeeStatus
END
GO

2. Model
ALTER PROCEDURE GetEmployeeStatus
AS
BEGIN
    SET NOCOUNT ON;
    SELECT        StatusID As EmployeeStatusID, Name As EmployeeStatusName, Cast(0 As Bit) as IsSelected
FROM            EmployeeStatus
END
GO

3.View
@model MVCChkBxTest.Models.CustomReport

@using (Html.BeginForm(null, null, FormMethod.Post))
{
    @Html.HiddenFor(x => x.SelectedCheckboxes, Model.SelectedCheckboxes)

    if(Model.statusList != null)
    {
        foreach(var item in Model.statusList)
        {
            @Html.HiddenFor(modelItem => item.EmployeeStatusID)
            @Html.HiddenFor(modelItem => item.IsSelected)
            @Html.CheckBoxFor(modelItem => item.IsSelected.Value,
                    new { @id = item.EmployeeStatusID, @class = "checkboxClass"})
            @Html.DisplayFor(modelItem => item.EmployeeStatusName)
        }
        <input type="submit" value="Search" name="btn" id="btnSearch" />
    }
}

@section Scripts{
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnSearch").click(function (evt) {
                var checkedVals = $('.checkboxClass:checkbox:checked').map(function () {
                    return this.id;
                }).get();
                $("#SelectedCheckboxes").val((checkedVals.join(',')));
            });
        });
    </script>   
}

4. Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCChkBxTest.Models;

namespace MVCChkBxTest.Controllers
{
    public class HomeController : Controller
    {
        CompanyEntities db = new CompanyEntities();
        public ActionResult Index()
        {
            CustomReport r = new CustomReport
            {
                statusList = db.GetEmployeeStatus()
            };
            return View(r);
        }

        [HttpPost]
        public ActionResult Index(CustomReport model, FormCollection c)
        {
            if (ModelState.IsValid)
            {
                int i = 0;
                int SelectedID = 0;
                bool isCheckboxSelected = false;

                var id = c.GetValues("item.EmployeeStatusID");
                var isSelected = c.GetValues("item.IsSelected");
                var selVal = c.GetValues("item.IsSelected.Value");
                for(i = 0; i < id.Count(); i++)
                {
                    SelectedID = Convert.ToInt32(id[i]);
                    isCheckboxSelected = Convert.ToBoolean(selVal[i]);
                }
                model.statusList = db.GetEmployeeStatus().ToList();
                if(model.SelectedCheckboxes != string.Empty)
                {
                    int[] nums = Array.ConvertAll(model.SelectedCheckboxes.Split(','),
                        int.Parse);
                    model.statusList = model.statusList.ToList();
                    List<GetEmployeeStatus_Result> res = (
                            from a in model.statusList.ToList()
                            join b in nums on a.EmployeeStatusID equals b
                            select a).ToList();
                    foreach(GetEmployeeStatus_Result r in res)
                    {
                        r.IsSelected = true;
                    }
                }
            }
            return View(model);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

No comments:

Post a Comment