简体   繁体   English

@ Html.DropDownList选择值问题

[英]@Html.DropDownList selected value issue

When I do submit I get StateID is always empty. 当我提交时,我得到的StateID始终为空。

Any clue what do I am missing here? 任何线索,我在这里想念什么?

THANK YOU! 谢谢!

HTML: 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>

Code: 码:

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

Model: 模型:

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

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

I found correct solution here @Html.DropDownList selected value issue 我在这里找到正确的解决方案@ Html.DropDownList选择值问题

So in my case it should be like: 所以在我的情况下应该是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM