MVC3 选定值
在我的模型中:
public SelectList QuestionGroupSelectList { get; set; }
------
List<QuestionGroup> questionGroupList = questionGroupRepository.GetQuestionGroup_BySurveyId(survey.Id);
Dictionary<int, string> questionGroupDictionary = questionGroupList.ToDictionary(l => l.Id, l => l.Name);
QuestionGroupSelectList = new SelectList(questionGroupDictionary, "key", "value", questionGroupId);
---------------------------------------
In view:
@Html.DropDownList("QuestionGroupSelectList", Model.QuestionGroupSelectList, "Choose Here")
当我调试时,我在 QuestionGroupSelectList 中得到 2 项(一项 Id 30,一项 Id 35),它说 selectedValue 是 35 (questionGroupId = 35)
但 selectedvalue 在视图中不起作用,任何想法?
提前致谢!
In my model:
public SelectList QuestionGroupSelectList { get; set; }
------
List<QuestionGroup> questionGroupList = questionGroupRepository.GetQuestionGroup_BySurveyId(survey.Id);
Dictionary<int, string> questionGroupDictionary = questionGroupList.ToDictionary(l => l.Id, l => l.Name);
QuestionGroupSelectList = new SelectList(questionGroupDictionary, "key", "value", questionGroupId);
---------------------------------------
In view:
@Html.DropDownList("QuestionGroupSelectList", Model.QuestionGroupSelectList, "Choose Here")
When i debug i get 2 items in QuestionGroupSelectList (one with Id 30 and one with Id 35), and it says that the selectedValue is 35 (questionGroupId = 35)
But the selectedvalue does not work in the view, any ideas?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用不同的属性来绑定下拉列表值。此外,您还应该使用视图模型和强类型助手,如下所示:
然后您可以有一个控制器操作来填充此视图模型并将其传递给视图:
最后在您的视图中:
You should use a different property to bind your dropdownlist value to. Also you should use view models and strongly typed helpers, like this:
then you could have a controller action which populates this view model and passes it to the view:
and finally in your view: