Rik*_*ous 2 razor asp.net-mvc-3 drop-down-menu
刚开始使用MVC3并尝试在视图中构建下拉列表时遇到问题.ViewModel填充了项目的SelectList:
mdm.CaptionSetList=new SelectList(CaptionSet.Fetch(), "CaptionSetId", "Description")
Run Code Online (Sandbox Code Playgroud)
然后在视图中使用:
@Html.DropDownListFor(model => model.Entity.CaptionSetId, model.CaptionSetList)
Run Code Online (Sandbox Code Playgroud)
但是当页面被点击时,该行会突出显示编译器消息:
编译器错误消息:CS0103:当前上下文中不存在名称"model"
我正在制作的初学者错误是什么?
DropDownListFor的第一个参数是一个函数,因此part是正确的,但第二个部分只是期望一个SelectList所以你需要做的就是
@Html.DropDownListFor(model => model.Entity.CaptionSetId, Model.CaptionSetList)
Run Code Online (Sandbox Code Playgroud)
注意大小写.
进一步澄清
在强类型视图中,Model是一个引用绑定到视图的模型的属性.由于第二个参数只是期望一个列表,并且您已指定Model具有名为CaptionSetList的属性,因此您将列表指定为Model.CaptionSetList.如果你把列表放在了ViewBag,那就放了ViewBag.CaptionSetList.
将此与第一个参数进行对比,第一个参数是一个函数,它接受与模型相同类型的一个参数.
| 归档时间: |
|
| 查看次数: |
2348 次 |
| 最近记录: |