Thursday, 28 December 2017

Bind (Populate) CheckBoxList from database using Model in ASP.Net MVC Razor

Bind (Populate) CheckBoxList from database using Model in ASP.Net MVC Razor

Description:

In this example we explain that how to bind CheckBoxList from database using Model in Asp.Net MVC.or how to populate CheckBoxList from Database using Model in MVC Razor. Or how to bind CheckBoxList Dynamically from database in MVC controller.

In asp.net there is an in built CheckBoxList control available but here in MVC there is no such type of control so to achieve this we use the SelectListItem class as Model and Custom CheckBoxList has been populated or bind from database in MVC Razor View.
Controller:

public class EmployeeController : Controller
    {

        public ActionResult Index()
        {
            List<SelectListItem> items = new List<SelectListItem>();
            string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = " SELECT GameName, GameId FROM Games";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            items.Add(new SelectListItem
                            {
                                Text = sdr["GameName"].ToString(),
                                Value = sdr["GameId"].ToString()
                            });
                        }
                    }
                    con.Close();
                }
            }

            return View(items);
        }

        [HttpPost]
        public ActionResult Index(List<SelectListItem> items)
        {
            ViewBag.Message = "Selected Game Items:\\n";
            foreach (SelectListItem item in items)
            {
                if (item.Selected)
                {
                    ViewBag.Message += string.Format("{0}\\n", item.Text);
                }
            }
            return View(items);
        }
    }

View: 

@model List<SelectListItem>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Bind (Populate) CheckBoxList from database using Model in ASP.Net MVC Razor</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Employee", FormMethod.Post))
    {
        <table>
            @for (int i = 0; i < Model.Count(); i++)
            {
            <tr>
                <td>
                    @Html.CheckBoxFor(m => m[i].Selected)
                </td>
                <td>
                    @Html.DisplayFor(m => m[i].Text)
                    @Html.HiddenFor(m => m[i].Value)
                    @Html.HiddenFor(m => m[i].Text)
                </td>
            </tr>
            }
        </table>
        <br/>
        <input type="submit" value="Submit"/>
    }
    @if (ViewBag.Message != null)
    {
        <script type="text/javascript">
            window.onload = function () {
                alert("@ViewBag.Message");
            };
        </script>
    }
</body>
</html>



This entry was posted in :

0 comments:

Post a Comment