ASP.NET MVC DropDownList SelectedValue 适用于编辑操作,但不适用于创建操作
我的控制器中有以下代码(用于编辑和创建):
model.Templates = new SelectList(PageManagementService.PageTemplateFetchList(), "PageId", "Title", 213);
“213”是其中一个页面的 ID - 只是用它来测试。
这是我的观点(对于编辑和创建):
<%= this.Html.DropDownListFor(model => model.Page.TemplateId, this.Model.Templates)%>
<%= this.Model.Templates.SelectedValue %>
当我转到创建表单时,我看到下拉列表,但未选择 value="213" 的标记。我什至输出 SelectedValue 以确保它是 213 - 我看到了 213。
当我转到“编辑”表单时,我会看到下拉列表,并且选择了 value="213" 的标签。
在“创建”表单上,所有标记都没有“选定”属性。
在“编辑”表单上,值为“213”的标签具有“selected”属性。
我错过了什么吗?可能是什么原因造成的?有人见过这种行为吗?
更新:更改下拉列表的名称使其正常工作。例如,
<%= this.Html.DropDownListFor(model => model.Page.TemplateId, this.Model.Templates)%>
我没有这样做,
<%= this.Html.DropDownList("somedropdown", this.Model.Templates)%>
而且它有效。虽然不知道为什么...
I have the following code in my controller (for Edit and Create):
model.Templates = new SelectList(PageManagementService.PageTemplateFetchList(), "PageId", "Title", 213);
the "213" is an Id for one of the pages - just using it for testing.
And this is in my view (for Edit and Create):
<%= this.Html.DropDownListFor(model => model.Page.TemplateId, this.Model.Templates)%>
<%= this.Model.Templates.SelectedValue %>
When I go to the Create form, I see the dropdown list, but the tag with value="213" is not selected. I even output the SelectedValue to make sure it's 213 - and I see 213.
When I go to the Edit form, I see the dropdown list, and the tag with value="213" is selected.
On the Create form, none of the tags have a "selected" attribute.
On the Edit form, the tag with value="213" has the "selected" attribute.
Am I missing something? What could be causing this? Anyone see this behavior before?
UPDATE: Changing the name of the dropdownlist makes it work. For example, instead of
<%= this.Html.DropDownListFor(model => model.Page.TemplateId, this.Model.Templates)%>
I did
<%= this.Html.DropDownList("somedropdown", this.Model.Templates)%>
and it worked. Not sure why though...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况的原因是
DataValueField
是一个字符串对象,并且可能存在类型不匹配。尝试这样的事情:
This could occur because the
DataValueField
is a string object and it might have a type mismatch there.Try something like this: