一尘不染

MVC5 Razor html.dropdownlist用于在值位于数组中时选择的设置

c#

我正在使用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数组中:fields
AggregationLevelConfiguration.CodeTypeAggregationLevelConfiguration.HelperCodeType

我想我必须在“视图”中设置选定的值,但是我不知道该怎么做。

如何设置选择的值?


阅读 566

收藏
2020-05-19

共1个答案

一尘不染

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

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

选项1 (使用EditorTemplate

EditorTemplate为集合中的类型创建一个自定义。创建局部输入/Views/Shared/EditorTemplates/AggregationLevelConfiguration.cshtml(请注意名称必须与类型名称匹配

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

然后在主视图中将传递SelectListEditorTemplateasadditionalViewData

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

选项2SelectList在每次迭代中生成一个新值并设置selectedValue

在此选项中,您的财产CodeTypeItems应为IEnumerable<GenericIdNameType>,而不是SelectList(或仅codeTypes作为公共财产)。然后在主视图中

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

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

2020-05-19