Monday, 25 December 2017

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

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

Description:

In this example we explain that how to Bind RadioButtonList from database using Model in Asp.Net MVC Razor view. Or how to populate RadioButtonList from database in MVC Razor.in previous example we already explain that how to bind or populate DropDownList from database in MVC but here we demonstrate that how to bind or populate RadioButtonList in MVC using Model.
Controller:

public class EmployeeController : Controller
    {

       // GET: Home
    public ActionResult Index()
    {
        List<SelectListItem> items = PopulateFruits();
        return View(items);
    }

    [HttpPost]
    public ActionResult Index(string fruit)
    {
        List<SelectListItem> items = PopulateFruits();
        var selectedItem = items.Find(p => p.Value == fruit);
        if (selectedItem != null)
        {
            selectedItem.Selected = true;
            ViewBag.Message = "Selected Fruit: " + selectedItem.Text;
        }

        return View(items);
    }

    private static List<SelectListItem> PopulateFruits()
    {
        List<SelectListItem> items = new List<SelectListItem>();
        string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            string query = " SELECT FruitName, FruitId FROM Fruits";
            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["FruitName"].ToString(),
                            Value = sdr["FruitId"].ToString()
                        });
                    }
                }
                con.Close();
            }
        }

        return items;
    }
}

View: 

@model List<SelectListItem>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Bind (Populate) RadioButtonList from database using Model in ASP.Net MVC Razor</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Employee", FormMethod.Post))
    {
        <table>
            @foreach(var m in Model)
            {
                <tr>
                    <td>
                        @Html.RadioButton("Fruit", m.Value, m.Selected, new {@id = m.Value })
                    </td>
                    <td>
                        @Html.Label(m.Text, new { @for = m.Value})
                    </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>


0 comments:

Post a Comment