栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

不幸的是

@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

然后在主视图中将传递

SelectList
EditorTemplate
as
additionalViewData

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

选项2

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

在此选项中,您的财产

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

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

旁注:无需使用

new { id = "Configurations[0].HelperCodeType"
-
DropDownListFor()
已经生成该
id
属性的方法



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470760.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号