Asp.net mvc 4 为什么在模型中无法检索dropdownlist值

Asp.net mvc 4 为什么在模型中无法检索dropdownlist值,asp.net-mvc-4,html.dropdownlistfor,Asp.net Mvc 4,Html.dropdownlistfor,我有一个表单,它使用mvc模型中设置的DropDownListFor。但奇怪的是,它们的当前值有时无法在模型中检索,有时可以。代码是 <% using (Html.BeginForm("EditPageSubmit", "Home", FormMethod.Post)) { <%: Html.DropDownListFor(model => model.Destination, Model.Destinations, Model.Destination)%> ... m

我有一个表单,它使用mvc模型中设置的DropDownListFor。但奇怪的是,它们的当前值有时无法在模型中检索,有时可以。代码是

<% using (Html.BeginForm("EditPageSubmit", "Home", FormMethod.Post))
{
<%: Html.DropDownListFor(model => model.Destination, Model.Destinations, Model.Destination)%>
...

model.Destinations= (from r in this._repository.Destinations
                                select new SelectListItem
                                {
                                    Text = r.Name,
                                    Value = SqlFunctions.StringConvert((double)r.DestinationID),
                                    Selected = false
                                });
Model.Destination = "...";

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditPageSubmit(FormCollection collection)
{
    var updatedModel = new SalesViewModel();

    if (TryUpdateModel(updatedModel))
    ...
}

奇怪的是,有时当dropdownlist没有更改并保留初始值时,UpdateModel.Destination为null,有时为所选值。我真的不知道这个问题,需要帮助。谢谢

我找到了原因。初始化变量不能与模型id相同。在这种情况下,只需更改为Html.DropDownListFormodel=>model.Destination、model.Destinations、model.Destination1即可

实体框架急切加载功能可能尚未加载数据。Is_repository.Destinations标记为虚拟。i、 e.标记为虚拟的目标集合属性。不,它不是虚拟的。我发现,如果更改为Html.DropDownListFormodel=>model.Destination,model.Destinations,即未由model.Destination初始化,则不会发生这种情况。但dropdownlist的初始值设置不正确。原因之一可能是使用单独的视图模型,而不是直接与数据访问模型绑定。可能是?无论如何,谢谢。