无法访问@ Html.DropDownList值

时间:2017-05-20 06:16:11

标签: c# asp.net-mvc

我是ASP.Net MVC的新手,试图使用viewbag绑定dropdownlist并在控制器中访问它,以下是我的代码

控制器构造函数

 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;
    }

还使用Model

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

查看

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

想做这样的事情

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

但是在下拉列表中使用m =&gt; m.State时会出错 如何使其在控制器中访问其值

更新: 在模型中声明了一个选择列表并尝试了DropdownlistFor

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

1 个答案:

答案 0 :(得分:0)

示例:

<强>型号:

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

<强>控制器:

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();
        }
    }

查看:

<强> 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" />
}