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

Last Reply one year ago By rani

Posted one year ago

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


Posted one year ago

Hi dharmendr

Good expalnation with sample. The dropdown sample is using model. How to bind the dropdown without using model?


Posted one year ago Modified on one year 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>

 


Posted one year ago

Thank you.

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html