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/17381635/

相关文章:

c# - 具有深层嵌套依赖关系的单元测试和依赖注入(inject)

c# - 在一个强类型的世界中,为什么 ASP.NET MVC 对命名约定的脆弱依赖不令人不悦?

c# - DropDownListFor 如何从数据库设置默认值

c# - Html.BeginForm 不将值返回到 Controller

razor - 在 Razor 局部 View 中保留数据

c# - 如何发送延续 token 来查询azure时间序列见解?

c# - 使用dapper时sql放在哪里?

c# - 使用 LINQ 填充列表

asp.net-mvc - MVC RouteUrl 不支持排除属性

asp.net-mvc - 带名称的 MVC @Html.ActionLink