如何在 ASP.NET MVC 和 C# 中从数据库中选择下拉列表的值?

发布于 2024-10-14 19:45:35 字数 1541 浏览 0 评论 0原文

我有一个 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 dropdownlistbut 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请爱~陌生人 2024-10-21 19:45:35

SelectList 采用选定的值以及可用选项。

所以这个例子应该选择MRS。

new SelectList(new[] { "MR", "MRS", "MISS", "MS", "SIR", "REV", "DR"}, "MRS")

更新

这是一个使用 RenderPartial 的示例...

<% Html.RenderPartial("ViewUserControl1", new DropDownModel {Name = "Title", SelectedValue = "MISS"});%>

您可以使用 Model.Title 而不是“MISS”...

这是 DropDownModel

public class DropDownModel
{
    public string Name { get; set; }

    public object SelectedValue { get; set; }
}

这是部分视图:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApplication2.Models.DropDownModel>" %>
<%= Html.DropDownList("test", new SelectList(new[] { "MR", "MRS", "MISS", "MS", "SIR", "REV", "DR"}, Model.SelectedValue)) %>

这只是一个快速模型,展示您如何做到这一点 - 您可能想要改进这个概念!

SelectList takes a selected value as well as available options.

So this example should select MRS.

new SelectList(new[] { "MR", "MRS", "MISS", "MS", "SIR", "REV", "DR"}, "MRS")

UPDATE

Here is an example using RenderPartial...

<% Html.RenderPartial("ViewUserControl1", new DropDownModel {Name = "Title", SelectedValue = "MISS"});%>

You can use Model.Title instead of "MISS"...

Here is the DropDownModel

public class DropDownModel
{
    public string Name { get; set; }

    public object SelectedValue { get; set; }
}

Here is the partial view:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApplication2.Models.DropDownModel>" %>
<%= Html.DropDownList("test", new SelectList(new[] { "MR", "MRS", "MISS", "MS", "SIR", "REV", "DR"}, Model.SelectedValue)) %>

This is just a quick mock-up that shows how you could do it - you might want to improve upon the concept!

旧人九事 2024-10-21 19:45:35

我认为问题在于属性名称和控件名称不匹配。尽管我没有测试过,但我怀疑选择控件最终被命名为 Title.TitleDropDown。这可以防止默认映射器分配该值。

将下拉控件更改为:

<%=Html.DropDownList("",new SelectList(new[] { "MR", "MRS", "MISS", "MS", "SIR", "REV", "DR"}))%>

应该可以解决问题。

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:

<%=Html.DropDownList("",new SelectList(new[] { "MR", "MRS", "MISS", "MS", "SIR", "REV", "DR"}))%>

should fix the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文