Populate DropDown, RadioButton, CheckBox from Database using model and without model in ASP.Net MVC

Last Reply one month ago By rani

Posted one month ago

How to display database record in dropdown, radiobutton, checkbox using model and without model in mvc view page.


Posted one month ago Modified on one month ago

Hi rani,

Refer the below code. You have to use ViewData to pass the list of items to the view page.

Controller

public ActionResult Index()
{
    List<SelectListItem> items = new List<SelectListItem>();
    // Populate from database.
    items.Add(new SelectListItem() { Text = "Test 1", Value = "1" });
    items.Add(new SelectListItem() { Text = "Test 2", Value = "2" });
    items.Add(new SelectListItem() { Text = "Test 3", Value = "3" });
    items.Add(new SelectListItem() { Text = "Test 4", Value = "4" });
    items.Add(new SelectListItem() { Text = "Test 5", Value = "5" });
    items.Add(new SelectListItem() { Text = "Test 6", Value = "6" });
    ViewData["ddl"] = items;
    return View(items);
}

View

<div>
    <%:Html.DropDownList("Test",(IEnumerable<SelectListItem>)ViewData["ddl"],"Select") %>
    <br />
    <br />
    <table border="1">
        <%foreach (SelectListItem item in (IEnumerable<SelectListItem>)ViewData["ddl"])
            {%>
        <tr>
            <td>
                <%:Html.CheckBox("Test", item.Selected, item.Value)%>
                <%:Html.Label(item.Text)%>
                <%:Html.Hidden(item.Value)%>
            </td>
            <td>
                <%:Html.RadioButton("Item", item.Value,item.Selected, "")%>
                <%:Html.Label(item.Text)%>
            </td>
        </tr>
        <%}%>
    </table>
</div>

 

I agree, here is the link: http://e-iceblue.com/free-api