Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DropDownList用于不从模型中预选值_C#_Asp.net Mvc - Fatal编程技术网

C# DropDownList用于不从模型中预选值

C# DropDownList用于不从模型中预选值,c#,asp.net-mvc,C#,Asp.net Mvc,控制器代码: AccountsDetailsViewModel vOwnerVM = Mapper.Map<VehicleOwner, AccountsDetailsViewModel>(vOwner); AccountsDetailsAccountViewModel vOwnerDetailsVM = Mapper.Map<VehicleOwner, AccountsDetailsAccountViewModel>(vOwner); ViewBag.FleetType

控制器代码:

AccountsDetailsViewModel vOwnerVM = Mapper.Map<VehicleOwner, AccountsDetailsViewModel>(vOwner);
AccountsDetailsAccountViewModel vOwnerDetailsVM = Mapper.Map<VehicleOwner, AccountsDetailsAccountViewModel>(vOwner);

ViewBag.FleetType = new SelectList(VehicleOwnerFleetTypeFactory.GetTypes().OrderBy(l => l.Type), "Id", "Type");
vOwnerVM.AccountDetails = vOwnerDetailsVM;
主视图代码

@model X.Views.Accounts.ViewModels.AccountsDetailsViewModel
@Html.Partial("DetailsAccount", @Model.AccountDetails)
@model X.Views.Accounts.ViewModels.AccountsDetailsAccountViewModel
@Html.DropDownListFor(model => model.FleetType, 
    (IEnumerable<SelectListItem>)ViewBag.FleetType)
@Html.DisplayFor(model => model.FleetType) (FOR DEBUG)
部分视图模型代码:

public class AccountsDetailsViewModel
{
    public AccountsDetailsAccountViewModel AccountDetails { get; set; }
}
[DisplayName("Fleet Type")]
public int FleetType { get; set; }
部分视图代码

@model X.Views.Accounts.ViewModels.AccountsDetailsViewModel
@Html.Partial("DetailsAccount", @Model.AccountDetails)
@model X.Views.Accounts.ViewModels.AccountsDetailsAccountViewModel
@Html.DropDownListFor(model => model.FleetType, 
    (IEnumerable<SelectListItem>)ViewBag.FleetType)
@Html.DisplayFor(model => model.FleetType) (FOR DEBUG)
无论数据库显示什么,下拉列表都不会显示模型中的正确值,只会显示列表中的顶部值

但是,如果我选择一个值,然后提交表单以将更改保存到数据库中,那么它确实会提交正确的值

当在控制器中返回viewmodel的视图时,这些值会准确地镜像数据库,我无法理解它为什么要这样做


编辑:“扇区”下拉列表是唯一一个自动选择正确值的下拉列表。

对我来说很好。检查是否将模型传递到视图

@using WebApplication2.Controllers
@model WebApplication2.Controllers.MyModel
@{
    ViewBag.Title = "Home Page";
    ViewBag.FleetType = new SelectList(
        VehicleOwnerFleetTypeFactory.GetTypes().OrderBy(l => l.Type),
        "Id", 
        "Type");
}

<div>
    <h2>---------------------------</h2>

    @Html.DropDownListFor(model => model.FleetType,
            (SelectList)ViewBag.FleetType)
    <h2>---------------------------</h2>
</div>

问题可能是由于您的selectlist和VM的成员变量具有相同的名称

model.FleetType 

(IEnumerable)ViewBag.FleetType

尝试将选择列表重命名为FleetTypeList或类似的

有一件事我忘了提到。它位于局部视图内。我将编辑上面的答案,以显示所涉及的代码。@Seb如果您的局部视图具有正确的模型,请签入debug它肯定是正确的,因为SectorId更新正确(即直接从数据库中提取值),但从工厂中提取其值的所有SelectList都不正确。我甚至可以显示这些值,并且它们是正确的,所以问题出在选择列表的某个地方@Seb您能否仅提供带有FleetType相关代码的干净样本(包括主视图和局部视图)。如果你有麻烦,请联系我。我可以。是的,我要把所有的问题都放在第一个问题上吗?
(IEnumerable<SelectListItem>)ViewBag.FleetType