@ Html.DropDownList选中的值问题

时间:2012-11-14 11:42:13

标签: c# asp.net-mvc

当我提交时,我得到StateID总是空的。

任何线索我在这里缺少什么?

谢谢!

HTML:

<div style="display: inline-block;">@Html.LabelFor(x => x.Common.StateID , new { @class = "SmallInput2" })</div>
<div style="display: inline-block;">@Html.DropDownList("StateID", Model.Common.StateList, "-- Select --",  new { style = "width: 130px;" })</div>

代码:

public ActionResult SignUp()
        {
            var model =  new SignUpModel
            {
                Common = new Common(),
                Individual = new Individual(),
                Business = new Business()
            };

            var stateList = new SelectList(_context.States.OrderBy(w => w.StateName), "ID", "StateName", _context.States.OrderBy(w => w.StateName).FirstOrDefault().ID);
            model.Common.StateList = stateList;
            model.Common.StateID = _context.States.OrderBy(w => w.StateName).FirstOrDefault().ID;

            return View(model);
        }

型号:

[Display(Name = "State")]
public Guid StateID { get; set; }

[Display(Name = "States")]
public SelectList StateList { get; set; }

1 个答案:

答案 0 :(得分:0)

我在@Html.DropDownList selected value issue

找到了正确的解决方案

所以在我的情况下应该是这样的:

   <div style="display: inline-block;">@Html.DropDownListFor(x => x.Common.StateID, Model.Common.StateList, "-- Select --",  new { style = "width: 130px;" })</div>