c# - MVC5 Razor html.dropdownlistfor 当值在数组中时设置选择

标签 c# asp.net-mvc razor

我正在使用 C# 和 .NET Framework 4.6.1 开发 ASP.NET MVC 5 应用程序。

我有这个 View :

@model MyProject.Web.API.Models.AggregationLevelConfViewModel

[...]

@Html.DropDownListFor(m => m.Configurations[0].HelperCodeType, (SelectList)Model.HelperCodeTypeItems, new { id = "Configurations[0].HelperCodeType" })

ViewModel 是:

public class AggregationLevelConfViewModel
{
    private readonly List<GenericIdNameType> codeTypes;
    private readonly List<GenericIdNameType> helperCodeTypes;

    public IEnumerable<SelectListItem> CodeTypeItems
    {
        get { return new SelectList(codeTypes, "Id", "Name"); }
    }

    public IEnumerable<SelectListItem> HelperCodeTypeItems
    {
        get { return new SelectList(helperCodeTypes, "Id", "Name"); }
    }

    public int ProductionOrderId { get; set; }

    public string ProductionOrderName { get; set; }

    public IList<Models.AggregationLevelConfiguration> Configurations { get; set; }

    public AggregationLevelConfViewModel()
    {
        // Load CodeTypes to show it as a DropDownList
        byte[] values = (byte[])Enum.GetValues(typeof(CodeTypes));

        codeTypes = new List<GenericIdNameType>();
        helperCodeTypes = new List<GenericIdNameType>();

        for (int i = 0; i < values.Length; i++)
        {
            GenericIdNameType cType = new GenericIdNameType()
            {
                Id = values[i].ToString(),
                Name = EnumHelper.GetDescription((CodeTypes)values[i])
            };

            if (((CodeTypes)values[i]) != CodeTypes.NotUsed)
                codeTypes.Add(cType);

            helperCodeTypes.Add(cType);
        }
    }
}

并且Models.AggregationLevelConfiguration是:

public class AggregationLevelConfiguration
{
    public byte AggregationLevelConfigurationId { get; set; }
    public int ProductionOrderId { get; set; }
    public string Name { get; set; }
    public byte CodeType { get; set; }
    public byte HelperCodeType { get; set; }
    public int PkgRatio { get; set; }
    public int RemainingCodes { get; set; }
}

我需要在这些属性中设置选定的值:

public IEnumerable<SelectListItem> CodeTypeItems
{
    get { return new SelectList(codeTypes, "Id", "Name"); }
}

public IEnumerable<SelectListItem> HelperCodeTypeItems
{
    get { return new SelectList(helperCodeTypes, "Id", "Name"); }
}

但我无法在 new SelectList(codeTypes, "Id", "Name");new SelectList(helperCodeTypes, "Id", "Name"); 中设置它 因为所选值位于 Configurations 数组中:字段 AggregationLevelConfiguration.CodeTypeAggregationLevelConfiguration.HelperCodeType

我想我必须在 View 中设置选定的值,但我不知道该怎么做。

如何设置选定的值?

最佳答案

不幸的是@Html.DropDownListFor()在循环中渲染控件时,其行为与其他助手略有不同。这之前已被报告为 CodePlex 上的一个问题(不确定这是一个错误还是只是一个限制)

有 2 个选项可以解决此问题,以确保根据模型属性选择正确的选项

选项 1(使用 EditorTemplate )

创建自定义EditorTemplate对于集合中的类型。在/Views/Shared/EditorTemplates/AggregationLevelConfiguration.cshtml中创建一个部分(注意名称必须与类型名称匹配

@model yourAssembly.AggregationLevelConfiguration
@Html.DropDownListFor(m => m.HelperCodeType, (SelectList)ViewData["CodeTypeItems"])
.... // other properties of AggregationLevelConfiguration

然后在主视图中,传递 SelectListEditorTemplateadditionalViewData

@using (Html.BeginForm())
{
  ...
  @Html.EditorFor(m => m.Configurations , new { CodeTypeItems = Model.CodeTypeItems })
  ...

选项 2(在每次迭代中生成新的 SelectList 并设置 selectedValue )

在此选项中,您的属性(property) CodeTypeItems应该是IEnumerable<GenericIdNameType> ,不是SelectList (或者只是将 codeTypes 设为公共(public)属性(property))。然后在主视图中

@Html.DropDownListFor(m => m.Configurations[0].HelperCodeType, new SelectList(Model.CodeTypeItems, "Id", "Name", Model.Configurations[0].HelperCodeType)

旁注:无需使用new { id = "Configurations[0].HelperCodeType" - DropDownListFor()方法已经生成了 id属性

关于c# - MVC5 Razor html.dropdownlistfor 当值在数组中时设置选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47916343/

相关文章:

c# - VB/C# 按键对对象进行分组 - 性能(内存)问题

asp.net-mvc - 具有复杂子属性的 Kendo 对象的模型绑定(bind)问题

c# - 如何使用 MSDN 文档和 Visual Studio 找出我需要的 "using"指令

razor - Umbraco - 使用 Razor 渲染 .Net 用户控件 (ascx) 宏

jquery - Url.Action 找不到 Controller

c# - 从最匹配的对象到最不匹配的对象排列对象

c# - 为什么会为异步/等待状态机生成 bool "flag"?

c# - 如何在 MVC 中使用 Nlog 将消息记录到 txt 文件?

asp.net-mvc - 没有为此对象定义无参数构造函数?

c# - 从多列检索数据并在单列中显示