Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net MVC DropDownList Data Binding

                    <form id="Form1" runat="server">
                        <asp:DropDownList ID="dvmDrmList" runat="server">
                            <asp:ListItem>Theory</asp:ListItem>
                            <asp:ListItem>Appliance</asp:ListItem>
                            <asp:ListItem>Lab</asp:ListItem>
                        </asp:DropDownList>
                    </form>

I want to bind this DropDownList in controller. I mean how can I get the value of the dropDownList in the action method in controller class. Thanks.

like image 570
Brucevilla Avatar asked Jan 21 '11 13:01

Brucevilla


People also ask

How do I bind a DropDownList in MVC model?

Binding MVC DropDownList with Static Values Just add an Html helper for DropDownList and provide a static list of SelectListItem. The values added as SelectListItem will be added and displayed in the DropDownList. In this way, you do not need to add anything to Controller Action.

How do you bind a dropdown value?

Data binding can be achieved by using the bind-Value attribute and it supports string, int, Enum and bool types. If component value has been changed, it will affect all the places where you bind the variable for the bind-value attribute.


1 Answers

I see that you are using forms with runat="server" and asp:XXX web controls. Those are notions should never be used in ASP.NET MVC. There is no more ViewState and PostBacks that those server controls rely on.

So in ASP.NET MVC you would start by defining a view model representing the data:

public class ItemsViewModel
{
    public string SelectedItemId { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

then you would define a controller with two actions (one that renders the view and another that handles the form submission):

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new ItemsViewModel
        {
            Items = new[]
            {
                new SelectListItem { Value = "Theory", Text = "Theory" },
                new SelectListItem { Value = "Appliance", Text = "Appliance" },
                new SelectListItem { Value = "Lab", Text = "Lab" }
            }
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(ItemsViewModel model)
    {
        // this action will be invoked when the form is submitted and 
        // model.SelectedItemId will contain the selected value
        ...
    }
}

and finally you would write the corresponding strongly typed Index view:

<%@ Page 
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<AppName.Models.ItemsViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% using (Html.BeginForm()) { %>
        <%= Html.DropDownListFor(x => x.SelectedItemId, new SelectList(Model.Items, "Value", "Text")) %>
        <input type="submit" value="OK" />
    <% } %>
</asp:Content>

This being said you could also hardcode this select inside your view (although this is something I wouldn't recommend):

<% using (Html.BeginForm()) { %>
    <select name="selectedItem">
        <option value="Theory">Theory</option>
        <option value="Appliance">Appliance</option>
        <option value="Lab">Lab</option>
    </select>
    <input type="submit" value="OK" />
<% } %>

and have the following controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(string selectedItem)
    {
        // this action will be invoked when the form is submitted and 
        // selectedItem will contain the selected value
        ...
    }
}
like image 134
Darin Dimitrov Avatar answered Jan 22 '23 21:01

Darin Dimitrov