如何修改asp.net-mvc的DropDownList,使其不选中SelectList中已选定的项?

2026-03-30 12:301阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计988个文字,预计阅读时间需要4分钟。

如何修改asp.net-mvc的DropDownList,使其不选中SelectList中已选定的项?

在使用ASP.NET MVC3应用中,我无法在特定编辑器视图中使用`DropDownListFor`来处理我的属性。我的模型是一个人,每个人都有一个属性,即人物类型。`PersonType`是一个包含名称/描述的包。

我正在使用ASP.NET MVC3应用程序,我无法在特定的编辑器视图中使用DropDownListFor来处理我的属性.

我的模型是一个人,一个人有一个属性,指定它的“人物类型”. PersonType是一个包含名称/描述和ID的类.我可以通过名为ApplicationSettings的静态/共享类访问应用程序中可用的PersonTypes.

在我的Person的Edit Template视图中,我创建了一个SelectList用于调试目的:

@ModelType MyNamespace.Person @Code Dim pTypesSelectList As New SelectList(MyNamespace.ApplicationSettings.PersonTypes, "ID", "Name", Model.PersonType.ID) End Code

然后我将此SelectList作为参数提供给DropDownListFor,该DropDownListFor绑定到Person的PersonType属性.

我还打印SelectList中每个项目的Selected属性以进行调试:

<div style="text-align: center; margin: 5px 0 0 0;"> <div> @Html.LabelFor(Function(model) model.PersonType) </div> <div> @Html.DropDownListFor(Function(model) model.PersonType, pTypesSelectList) @Html.ValidationMessageFor(Function(model) model.PersonType) <br /> <br /> <!-- The following is debugging code that shows the actual value--> @Model.Type.Name <br /> @Model.Type.ID <br /> <br /> <!--This section is to show that the select list has properly selected the value--> @For Each pitem In pTypesSelectList @<div> @pitem.Text selected: @pitem.Selected </div> Next </div> </div>

该视图绑定到PersonType属性为“Person Type#2”的Person,我希望这个被选中;但是此代码的HTML输出如下所示:

<div style="text-align: center; margin: 5px 0 0 0;"> <div> <label for="PersonType">PersonType</label> </div> <div> <select id="PersonType" name="PersonType"> <option value="7e750688-7e00-eeee-0000-007e7506887e">Default Person Type</option> <option value="87e5f686-990e-5151-0151-65fa7506887e">Person Type # 1</option> <option value="a7b91cb6-2048-4b5b-8b60-a1456ba4134a">Person Type # 2</option> <option value="8a147405-8725-4b53-b4b8-3541c2391ca9">Person Type # 3</option> </select> <span class="field-validation-valid" data-valmsg-for="PersonType" data-valmsg-replace="true"></span> <br /> <br /> <!-- The following is debugging code that shows the actual value--> Person Type # 2 <br /> a7b91cb6-2048-4b5b-8b60-a1456ba4134a <br /> <br /> <!--This section is to show that the select list has properly selected the value--> <div> Default Person Type selected: False </div> <div> Person Type # 1 selected: False </div> <div> Person Type # 2 selected: True </div> <div> Person Type # 3 selected: False </div> </div> </div>

如您所见,SelectList中项目的已打印Selected属性显示第3项是“Selected”.但令我发疯的是,与此相对应的选项是Not Selected.

通常,除非没有其他选项,否则HTML帮助程序将完全忽略SelectList中的Selected属性.如果DropDownListFor可以通过其他方式找到该值,它将坚持使用该值.

在这种情况下,它将使用model.PersonType(.ToString())的值 – 但这不是您想要的,通过您传递给SelectList的model.PersonType.ID来判断.

更多信息在the answer here.

解决方法

一个应该工作的简单解决方法是设置:

ViewData["PersonType"] = model.PersonType.Id.

如果存在,帮助程序首先在ModelState中查找 – 即在POST时.这应该已经有效,因为ModelState [“PersonType”]将填充已发布的实际选定值.

在ModelState之后,它将在ViewData中查找 – 首先使用ViewData [“PersonType”],然后只查看ViewData.Model.PersonType.换句话说,您可以使用直接在ViewData上设置的值“覆盖”模型上的值.

更好(IMO)的解决方案

更通用的“更好的实践”,解决它的方法(也避免使用自定义模型绑定器将POST的ID转换回PersonType)是使用ViewModel而不是在视图中使用完整模型:

如何修改asp.net-mvc的DropDownList,使其不选中SelectList中已选定的项?

>拥有PersonTypeID属性 – 而不是PersonType.
>使用PersonType.ID填充它
>在你的视图中使用它

> VB.NET:Html.DropDownListFor(Function(model)model.PersonTypeID),或
> C#:Html.DropDownListFor(model => model.PersonTypeID)

>当表单被POST时,将ViewModel(包括PersonTypeID => PersonType)转换回POST Action中的实际模型.

这似乎更多的工作,但通常在项目中有很多场合需要更多特定于视图的数据表示,以避免过多的内联Razor代码 – 所以从业务对象转换到视图模型,尽管它看起来很像有时多余和反干,往往会让你免于很多麻烦.

本文共计988个文字,预计阅读时间需要4分钟。

如何修改asp.net-mvc的DropDownList,使其不选中SelectList中已选定的项?

在使用ASP.NET MVC3应用中,我无法在特定编辑器视图中使用`DropDownListFor`来处理我的属性。我的模型是一个人,每个人都有一个属性,即人物类型。`PersonType`是一个包含名称/描述的包。

我正在使用ASP.NET MVC3应用程序,我无法在特定的编辑器视图中使用DropDownListFor来处理我的属性.

我的模型是一个人,一个人有一个属性,指定它的“人物类型”. PersonType是一个包含名称/描述和ID的类.我可以通过名为ApplicationSettings的静态/共享类访问应用程序中可用的PersonTypes.

在我的Person的Edit Template视图中,我创建了一个SelectList用于调试目的:

@ModelType MyNamespace.Person @Code Dim pTypesSelectList As New SelectList(MyNamespace.ApplicationSettings.PersonTypes, "ID", "Name", Model.PersonType.ID) End Code

然后我将此SelectList作为参数提供给DropDownListFor,该DropDownListFor绑定到Person的PersonType属性.

我还打印SelectList中每个项目的Selected属性以进行调试:

<div style="text-align: center; margin: 5px 0 0 0;"> <div> @Html.LabelFor(Function(model) model.PersonType) </div> <div> @Html.DropDownListFor(Function(model) model.PersonType, pTypesSelectList) @Html.ValidationMessageFor(Function(model) model.PersonType) <br /> <br /> <!-- The following is debugging code that shows the actual value--> @Model.Type.Name <br /> @Model.Type.ID <br /> <br /> <!--This section is to show that the select list has properly selected the value--> @For Each pitem In pTypesSelectList @<div> @pitem.Text selected: @pitem.Selected </div> Next </div> </div>

该视图绑定到PersonType属性为“Person Type#2”的Person,我希望这个被选中;但是此代码的HTML输出如下所示:

<div style="text-align: center; margin: 5px 0 0 0;"> <div> <label for="PersonType">PersonType</label> </div> <div> <select id="PersonType" name="PersonType"> <option value="7e750688-7e00-eeee-0000-007e7506887e">Default Person Type</option> <option value="87e5f686-990e-5151-0151-65fa7506887e">Person Type # 1</option> <option value="a7b91cb6-2048-4b5b-8b60-a1456ba4134a">Person Type # 2</option> <option value="8a147405-8725-4b53-b4b8-3541c2391ca9">Person Type # 3</option> </select> <span class="field-validation-valid" data-valmsg-for="PersonType" data-valmsg-replace="true"></span> <br /> <br /> <!-- The following is debugging code that shows the actual value--> Person Type # 2 <br /> a7b91cb6-2048-4b5b-8b60-a1456ba4134a <br /> <br /> <!--This section is to show that the select list has properly selected the value--> <div> Default Person Type selected: False </div> <div> Person Type # 1 selected: False </div> <div> Person Type # 2 selected: True </div> <div> Person Type # 3 selected: False </div> </div> </div>

如您所见,SelectList中项目的已打印Selected属性显示第3项是“Selected”.但令我发疯的是,与此相对应的选项是Not Selected.

通常,除非没有其他选项,否则HTML帮助程序将完全忽略SelectList中的Selected属性.如果DropDownListFor可以通过其他方式找到该值,它将坚持使用该值.

在这种情况下,它将使用model.PersonType(.ToString())的值 – 但这不是您想要的,通过您传递给SelectList的model.PersonType.ID来判断.

更多信息在the answer here.

解决方法

一个应该工作的简单解决方法是设置:

ViewData["PersonType"] = model.PersonType.Id.

如果存在,帮助程序首先在ModelState中查找 – 即在POST时.这应该已经有效,因为ModelState [“PersonType”]将填充已发布的实际选定值.

在ModelState之后,它将在ViewData中查找 – 首先使用ViewData [“PersonType”],然后只查看ViewData.Model.PersonType.换句话说,您可以使用直接在ViewData上设置的值“覆盖”模型上的值.

更好(IMO)的解决方案

更通用的“更好的实践”,解决它的方法(也避免使用自定义模型绑定器将POST的ID转换回PersonType)是使用ViewModel而不是在视图中使用完整模型:

如何修改asp.net-mvc的DropDownList,使其不选中SelectList中已选定的项?

>拥有PersonTypeID属性 – 而不是PersonType.
>使用PersonType.ID填充它
>在你的视图中使用它

> VB.NET:Html.DropDownListFor(Function(model)model.PersonTypeID),或
> C#:Html.DropDownListFor(model => model.PersonTypeID)

>当表单被POST时,将ViewModel(包括PersonTypeID => PersonType)转换回POST Action中的实际模型.

这似乎更多的工作,但通常在项目中有很多场合需要更多特定于视图的数据表示,以避免过多的内联Razor代码 – 所以从业务对象转换到视图模型,尽管它看起来很像有时多余和反干,往往会让你免于很多麻烦.