asp.net-mvc DropDownList 设置asp.net MVC 中的选定项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2410543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DropDownList setting selected item in asp.net MVC
提问by Gabe
I noticed what seems to me a bug in asp.net MVC or simply I am doing something wrong. I am currently using 1.0 so maybe this is something that will be addressed in the 2.0 release. But either way, here we go.
我注意到在我看来 asp.net MVC 中的一个错误,或者只是我做错了什么。我目前使用的是 1.0,所以这可能会在 2.0 版本中解决。但不管怎样,我们走了。
When I my view model has a property which is the same name as the declared id for a drop down list, the selected item is ignored and the rendered html has nothing selected. Not sure if I did something wrong, but changing the name of the id fixes the problem. I simplified the example, hope it is clear, otherwise please let me know.
当我的视图模型具有与下拉列表声明的 id 同名的属性时,所选项目将被忽略并且呈现的 html 没有选择任何内容。不确定我是否做错了什么,但更改 id 的名称可以解决问题。我简化了示例,希望清楚,否则请告诉我。
Here is my view where the declared ID is the same name as my list in the model:
这是我的视图,其中声明的 ID 与模型中的列表名称相同:
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<%= Html.DropDownList("IsMultipleServicers", Model.IsMultipleServicers) %>
</td>
</tr>
</table>
And the rendered Html
和渲染的 Html
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<select id="IsMultipleServicers" name="IsMultipleServicers">
<option value="false">No</option>
<option value="true">Yes</option>
</select>
</td>
</tr>
</table>
Now lets make a small change. I will change the declared id to be something different.
现在让我们做一个小小的改变。我会将声明的 id 更改为不同的东西。
Here is my View:
这是我的观点:
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<%= Html.DropDownList("MultipleServicers", Model.IsMultipleServicers) %>
</td>
</tr>
</table>
And now the rendered html:
现在呈现的html:
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<select id="IsMultipleServicers" name="IsMultipleServicers">
<option value="false">No</option>
<option selected="selected" value="true">Yes</option>
</select>
</td>
</tr>
</table>
Notice that now I get a selected option which would be the second element in the List.
请注意,现在我得到了一个选定的选项,它将是列表中的第二个元素。
Here is my ViewModel just to tie everything together:
这是我的 ViewModel 只是为了将所有内容联系在一起:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCProject.Models.ViewModels.Service
{
public class ServiceViewModel : ViewModel
{
public List<SelectListItem> IsMultipleServicers { get; set; }
}
}
Here is my action:
这是我的行动:
[AcceptVerbs(HttpVerbs.Get)]
public virtual ActionResult Service()
{
return View(new ServiceViewModel()
{
IsMultipleServicers = BuildBooleanSelectList(true)
};
}
private List<SelectListItem> BuildBooleanSelectList(bool isTrue)
{
List<SelectListItem> list = new List<SelectListItem>();
if (isTrue)
{
list.Add(new SelectListItem() { Selected = false, Text = "No", Value = "false" });
list.Add(new SelectListItem() { Selected = true, Text = "Yes", Value = "true" });
}
else
{
list.Add(new SelectListItem() { Selected = true, Text = "No", Value = "false" });
list.Add(new SelectListItem() { Selected = false, Text = "Yes", Value = "true" });
}
return list;
}
回答by Richard Szalay
I think the problem is a confusion regarding the DropDownListoverloads:
我认为问题是关于DropDownList重载的混淆:
Html.DropDownList(string name)looks for a view model property ofnameand typeIEnumerable<SelectListItem>. It will use the selected item (SelectListItem.Selected == true) from the list, unless there is a form post value of the same name.Html.DropDownList(string name, IEnumerable<SelectListItem> selectList)uses the items fromselectList, but not their selected values. The selected is found by resolvingnamein the view model (or post data) and matching it against theSelectListItem.Value. Even if the value cannot be found (or is null), it still won't use the selected value from the list of SelectListItems.
Html.DropDownList(string name)查找name和 类型 的视图模型属性IEnumerable<SelectListItem>。它将使用SelectListItem.Selected == true列表中的选定项 ( ),除非存在同名的表单发布值。Html.DropDownList(string name, IEnumerable<SelectListItem> selectList)使用 中的项目selectList,但不使用它们的选定值。通过name在视图模型(或发布数据)中解析并将其与SelectListItem.Value. 即使找不到该值(或为空),它仍然不会使用 SelectListItems 列表中的选定值。
Your code uses the second overload, but specifies a "value" property that doesn't exist ("MultipleServicers").
您的代码使用第二个重载,但指定了一个不存在的“值”属性(“MultipleServicers”)。
To fix your problem, either use the first overload:
要解决您的问题,请使用第一个重载:
<%= Html.DropDownList("IsMultipleServicers") %>
Or, add a string MultipleServicersproperty to your view model and populate it in your controller. I'd recommend this solution as it gets around several problems with initial display, post display and mapping the post data to a view/post model:
或者,string MultipleServicers向您的视图模型添加一个属性并将其填充到您的控制器中。我推荐这个解决方案,因为它解决了初始显示、后期显示和将后期数据映射到视图/后期模型的几个问题:
public class ServiceViewModel : ViewModel
{
public string MultipleServicers { get; set; }
public List<SelectListItem> IsMultipleServicers { get; set; }
}
Then for your HTML:
然后对于您的 HTML:
<%= Html.DropDownList(Model.MultipleServicers, Model.IsMultipleServicers) %>
This technique maps into MVC2, as well:
此技术也映射到 MVC2:
<%= Html.DropDownListFor(x => x.MultipleServicers, Model.IsMultipleServicers) %>
回答by Tim
I encountered this same problem using the Html.DropDownList(string name, IEnumerable selectList) overload. It appears that my model has a property of the same name as the name of the drop down list. This being the case, MVC favored the property value of my Model over the Selected property of each entry in the IEnumerable.
我在使用 Html.DropDownList(string name, IEnumerable selectList) 重载时遇到了同样的问题。看来我的模型具有与下拉列表名称同名的属性。在这种情况下,MVC 更喜欢我的模型的属性值,而不是 IEnumerable 中每个条目的 Selected 属性。
The solution was to use a name for the dropdown list that does not match up to a property name. Another solution would be to write my own extension method that ignores model and view state and instead always honor the selected property.
解决方案是使用与属性名称不匹配的下拉列表名称。另一种解决方案是编写我自己的扩展方法,忽略模型和视图状态,而是始终遵循所选属性。
回答by tvanfosson
The DropDownList helper pulls the default value from the model. In the first case, the value in the model corresponding to the name is a SelectList -- this doesn't match any of the items in the list, it is the list, so no value is chosen. In the second example, your model does not include a property with that name so the value from the model can't be used and it defaults to the state indicated in the SelectList itself. Typically, I will have a property on the model for the selected value -- this becomes the default -- and another property representing the potential values for the list.
DropDownList 助手从模型中提取默认值。在第一种情况下,模型中与名称对应的值是 SelectList —— 这与列表中的任何项目都不匹配,它是列表,因此没有选择任何值。在第二个示例中,您的模型不包含具有该名称的属性,因此无法使用模型中的值,并且它默认为 SelectList 本身中指示的状态。通常,我将在模型上为所选值设置一个属性——这将成为默认值——以及另一个表示列表潜在值的属性。

