选择MVC5剃刀html.dropdownlistfor设定值时,在数组 [英] MVC5 Razor html.dropdownlistfor set selected when value is in array
问题描述
我开发一个ASP.NET MVC应用5,用C#和.NET Framework 4.6.1。
I'm developing an ASP.NET MVC 5 application, with C# and .NET Framework 4.6.1.
我有这样的查看
:
@model MyProject.Web.API.Models.AggregationLevelConfViewModel
[...]
@Html.DropDownListFor(m => m.Configurations[0].HelperCodeType, (SelectList)Model.HelperCodeTypeItems, new { id = "Configurations[0].HelperCodeType" })
的视图模型
是:
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; }
}
我需要这些属性来设置选择的值:
I need to set selected value in these properties:
public IEnumerable<SelectListItem> CodeTypeItems
{
get { return new SelectList(codeTypes, "Id", "Name"); }
}
public IEnumerable<SelectListItem> HelperCodeTypeItems
{
get { return new SelectList(helperCodeTypes, "Id", "Name"); }
}
但我不能将其设置在新的SelectList(codetypes的,ID,姓名);
或新的SelectList (辅助codetypes的,ID,姓名);
,因为选择的值是配置
数组:田 AggregationLevelConfiguration。codeTYPE
和 AggregationLevelConfiguration.Helper codeTYPE
。
But I can't set it in new SelectList(codeTypes, "Id", "Name");
or new SelectList(helperCodeTypes, "Id", "Name");
because the selected value are in Configurations
array: fields AggregationLevelConfiguration.CodeType
and AggregationLevelConfiguration.HelperCodeType
.
我觉得我有在视图中选择设定值,但我不知道该怎么办了。
I think I have to set selected value in the View, but I don't know how to do it.
我怎么可以设置选择的值?
How can I set the selected values?
推荐答案
不幸的是 @ Html.DropDownListFor()
在一个循环中呈现控件时的表现有点不同于其他佣工。这一直是pviously报告为C $ CPLEX在$一个问题$ P $(不知道它的一个bug或只是一个限制)
Unfortunately @Html.DropDownListFor()
behaves a little differently than other helpers when rendering controls in a loop. This has been previously reported as an issue on CodePlex (not sure if its a bug or just a limitation)
该是解决此,以确保正确的选项2选项是基于模型属性选择了
The are 2 option to solve this to ensure the correct option is selected based on the model property
选项1 (使用 EditorTemplate
)
创建自定义的 EditorTemplate
为集合中的类型。在 /Views/Shared/EditorTemplates/AggregationLevelConfiguration.cshtml
创建一个部分(注意名称必须与类的名称相匹配
Create a custom EditorTemplate
for the type in the collection. Create a partial in /Views/Shared/EditorTemplates/AggregationLevelConfiguration.cshtml
(note the name must match the name of the type
@model yourAssembly.AggregationLevelConfiguration
@Html.DropDownListFor(m => m.HelperCodeType, (SelectList)ViewData["CodeTypeItems"])
.... // other properties of AggregationLevelConfiguration
,然后在主视图,通过的SelectList
到 EditorTemplate
为 additionalViewData
@using (Html.BeginForm())
{
...
@Html.EditorFor(m => m.Configurations , new { CodeTypeItems = Model.CodeTypeItems })
...
选项2 (生成一个新的的SelectList
在每次迭代中,并设置了selectedValue
)
Option 2 (generate a new SelectList
in each iteration and set the selectedValue
)
在这个选项你的财产 codeTypeItems
应该是的IEnumerable&LT; GenericIdNameType&GT;
,而不是的SelectList
(或者只是让 codetypes的
的公共属性)。然后在主视图
In this option your property CodeTypeItems
should to be IEnumerable<GenericIdNameType>
, not a SelectList
(or just make codeTypes
a public property). Then in the main view
@Html.DropDownListFor(m => m.Configurations[0].HelperCodeType, new SelectList(Model.CodeTypeItems, "Id", "Name", Model.Configurations[0].HelperCodeType)
边注:没有必要使用新{ID =配置[0] .Helper codeTYPE
- 在 DropDownListFor ()
方法已经产生了 ID
属性
Side note: there is no need to use new { id = "Configurations[0].HelperCodeType"
- the DropDownListFor()
method already generated that id
attribute
这篇关于选择MVC5剃刀html.dropdownlistfor设定值时,在数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!