Saurabh Saurabh -4 years ago 54
C# Question

Cant Access @Html.DropDownList value

I am new in ASP.Net MVC , trying to bind dropdownlist using viewbag and accessing it in controller ,
below is my code

Controller constructor

public HomeController()
{
List<SelectListItem> list = new List<SelectListItem>();
list.Add(new SelectListItem { Text="HO", Value="HO" });
list.Add(new SelectListItem { Text = "BIHAR", Value = "BIHAR" });
list.Add(new SelectListItem { Text = "CHHATTISGARH", Value = "CHHATTISGARH" });
list.Add(new SelectListItem { Text = "HARYANA", Value = "HARYANA" });
list.Add(new SelectListItem { Text = "Madhya PRADESH", Value = "Madhya PRADESH" });
list.Add(new SelectListItem { Text = "PUNJAB", Value = "PUNJAB" });
list.Add(new SelectListItem { Text = "RAJASTHAN", Value = "RAJASTHAN" });
list.Add(new SelectListItem { Text = "UTTAR PRADESH (E)", Value = "UTTAR PRADESH (E)" });
list.Add(new SelectListItem { Text = "UTTAR PRADESH (W)", Value = "UTTAR PRADESH (W)" });
list.Add(new SelectListItem { Text = "UTTARAKHAND", Value = "UTTARAKHAND" });
ViewBag.StateList = list;
}


Also using Model

public class BR_Model
{
public string State;
public DateTime date;
}


View

@Html.DropDownList("StateList", ViewBag.StateList as IEnumerable<SelectListItem>, new { @class = "form-control input-sm" })


wanna do something like this

@Html.DropDownList( here m => m.State , ViewBag.StateList as IEnumerable<SelectListItem>, new { @class = "form-control input-sm" })


But getting error when using m=>m.State in dropdownlist
How to make it work to access its value in controller

Update :
Declared a selectlist in model and tried DropdownlistFor

@Html.DropDownListFor(m=>m.State,Model.list as IEnumerable<SelectListItem> ,new { @class = "form-control input-sm" })

Answer Source

Example:

Model:

public class BR_Model
{
    public string State { get; set; };
    public DateTime Date { get; set; };
}

Controller:

public HomeController()
    {
        [HttpGet]
        public ActionResult Create()
        {
            var list = new List<SelectListItem>
            {
                new SelectListItem {Text = "HO", Value = "HO"},
                new SelectListItem {Text = "BIHAR", Value = "BIHAR"},
                new SelectListItem {Text = "CHHATTISGARH", Value = "CHHATTISGARH"},
                new SelectListItem {Text = "HARYANA", Value = "HARYANA"},
                new SelectListItem {Text = "Madhya PRADESH", Value = "Madhya PRADESH"},
                new SelectListItem {Text = "PUNJAB", Value = "PUNJAB"},
                new SelectListItem {Text = "RAJASTHAN", Value = "RAJASTHAN"},
                new SelectListItem {Text = "UTTAR PRADESH (E)", Value = "UTTAR PRADESH (E)"},
                new SelectListItem {Text = "UTTAR PRADESH (W)", Value = "UTTAR PRADESH (W)"},
                new SelectListItem {Text = "UTTARAKHAND", Value = "UTTARAKHAND"}
            };
            ViewBag.StateList = list;
            return View();
        }

        [HttpPost]
        public ActionResult Create(BR_Model model)
        {
          return View();
        }
    }

View:

Create.cshtml:

@model BR_Model 

@using (Html.BeginForm("Create", "Home"))//Or use Ajax.BeginForm if you need
{
   <div class="form-group">
    @Html.DisplayNameFor(m => m.State )
    @Html.DropDownListFor(m => m.State , new SelectList((IEnumerable)ViewBag.StateList,"Value","Text",Model.State ))
</div>
    <input type="submit" value="Save" />
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download