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

然后在主视图中,传递 SelectListEditorTemplate作为additionalViewData

@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/37407811/

相关文章:

asp.net-mvc - 关注点分离存储库模式和 Entity Framework 3.5

c# - 无法在 Razor View 中获取枚举显示名称属性

c# - 在循环突然停止的特定路径中创建 (N) 个目录

javascript - 从 JsonResult 返回字符串数组并在 JavaScript 中使用它

javascript - 如何从 JSon 方法接收数据?

xml - Visual Studio XML 文件编辑器小写

c# - MVC 3 - 如何从操作方法返回显示模板?

c# - 如何在C#中实现PsPing TCP ping

c# - 应用程序退出代码始终返回 0

c# - 如何获取请求的 SessionStateBehavior?