如何在 ASP.NET MVC 和 C# 中从数据库中选择下拉列表的值?
我有一个 dropdownlist
存储在 TitleEditorTemplate
中:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%=Html.DropDownList("",
new SelectList(
new[] { "MR", "MRS", "MISS", "MS", "SIR", "REV", "DR"}
)
)%>
查看
<%=Html.EditorFor(x => x.Title, "TitleEditorTemplate")%>
ViewModel
[UIHint("TitleEditorTemplate")]
[Required(ErrorMessage = "Please select a title")]
public string Title { get; set; }
控制器
//Results of a query of textboxes
Title = data.Title,
问题
当我尝试编辑(从数据库中提取数据)信息然后写入时,就会出现问题对数据库进行 UPDATE
后,其他所有内容都会正确拉回并插入到正确的文本框中。
但是,下拉列表
会自动选择第一个选项“MR”而不是“MRS”。
我知道它一定与它生成下拉列表的方式有关,但我不知道如何解决这个问题。
DropDownList 代码
<select id="Title_TitleDropDown" name="Title.TitleDropDown"><option>MR</option>
<option>MRS</option>
<option>MISS</option>
<option>MS</option>
<option>SIR</option>
<option>REV</option>
<option>DR</option>
</select>
我缺少什么?如何让它选择从数据库中选择的选项作为默认选择?
例如:
数据库列
- 标题:DR
- 姓名:John
- 姓氏:Smith
当用户想要更新此信息时,他们可以这样做。在我的 aspx 页面中,它将填充名字/姓氏文本框,但不会填充下拉列表,这将成为“MR”的默认值,因此名称变为 John Smith 先生而不是 John Smith 博士。
I have a dropdownlist
which is stored within TitleEditorTemplate
:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%=Html.DropDownList("",
new SelectList(
new[] { "MR", "MRS", "MISS", "MS", "SIR", "REV", "DR"}
)
)%>
View
<%=Html.EditorFor(x => x.Title, "TitleEditorTemplate")%>
ViewModel
[UIHint("TitleEditorTemplate")]
[Required(ErrorMessage = "Please select a title")]
public string Title { get; set; }
Controller
//Results of a query of textboxes
Title = data.Title,
Problem
My problem occurs when I'm trying to edit (pulling data from a database) the information and then write an UPDATE
to the database, everything else is pulled back correctly and inserted into the correct textboxes.
However the dropdownlist
automatically selects the first option 'MR' rather than 'MRS'.
I know it must have something to do with how it generates the dropdownlist
but I don't know how to fix this.
DropDownList Code
<select id="Title_TitleDropDown" name="Title.TitleDropDown"><option>MR</option>
<option>MRS</option>
<option>MISS</option>
<option>MS</option>
<option>SIR</option>
<option>REV</option>
<option>DR</option>
</select>
What am I missing? How do I get it to choose the option selected from the database as the default selection?
For example:
Database columns
- Title: DR
- Forename: John
- Surname: Smith
When the user wants to update this information, they can do so. Within my aspx page it will popluate both the forename/surname textboxes however not the dropdownlist, this instead becomes the default value of "MR" so the name becomes Mr John Smith rather than Dr John Smith.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SelectList 采用选定的值以及可用选项。
所以这个例子应该选择MRS。
更新
这是一个使用 RenderPartial 的示例...
您可以使用 Model.Title 而不是“MISS”...
这是 DropDownModel
这是部分视图:
这只是一个快速模型,展示您如何做到这一点 - 您可能想要改进这个概念!
SelectList takes a selected value as well as available options.
So this example should select MRS.
UPDATE
Here is an example using RenderPartial...
You can use Model.Title instead of "MISS"...
Here is the DropDownModel
Here is the partial view:
This is just a quick mock-up that shows how you could do it - you might want to improve upon the concept!
我认为问题在于属性名称和控件名称不匹配。尽管我没有测试过,但我怀疑选择控件最终被命名为 Title.TitleDropDown。这可以防止默认映射器分配该值。
将下拉控件更改为:
应该可以解决问题。
I believe the problem is that the property name and the control name don't match. I suspect, though I haven't tested, that the select control is ultimately named Title.TitleDropDown. This prevents the default mapper from assigning the value.
Changing your drop down control to:
should fix the problem.