DropDownList 设置asp.net MVC 中的选定项 [英] DropDownList setting selected item in asp.net MVC
问题描述
我注意到在我看来是 asp.net MVC 中的一个错误,或者只是我做错了什么.我目前使用的是 1.0,所以这可能会在 2.0 版本中解决.但无论如何,我们开始吧.
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.
当我的视图模型具有与下拉列表声明的 id 同名的属性时,所选项目将被忽略并且呈现的 html 没有选择任何内容.不确定我是否做错了什么,但更改 id 的名称可以解决问题.我简化了示例,希望清楚,否则请告诉我.
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 与模型中的列表名称相同:
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<%= Html.DropDownList("IsMultipleServicers", Model.IsMultipleServicers) %>
</td>
</tr>
</table>
和渲染的 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>
现在让我们做一个小小的改变.我会将声明的 id 更改为不同的内容.
这是我的观点:
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<%= Html.DropDownList("MultipleServicers", Model.IsMultipleServicers) %>
</td>
</tr>
</table>
现在呈现的 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.
这是我的 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; }
}
}
这是我的操作:
[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;
}
推荐答案
我认为问题是关于 DropDownList
重载的混淆:
I think the problem is a confusion regarding the DropDownList
overloads:
Html.DropDownList(string name)
查找name
和类型IEnumerable
的视图模型属性.它将使用列表中的选定项 (SelectListItem.Selected == true
),除非存在同名的表单发布值.
Html.DropDownList(string name)
looks for a view model property ofname
and 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
使用 selectList
中的项目,但不使用它们的选定值.通过解析视图模型(或发布数据)中的 name
并将其与 SelectListItem.Value
进行匹配,可以找到所选对象.即使找不到该值(或为空),它仍然不会使用 SelectListItems 列表中的选定值.
Html.DropDownList(string name, IEnumerable<SelectListItem> selectList)
uses the items from selectList
, but not their selected values. The selected is found by resolving name
in the view model (or post data) and matching it against the SelectListItem.Value
. Even if the value cannot be found (or is null), it still won't use the selected value from the list of SelectListItems.
您的代码使用了第二个重载,但指定了一个不存在的value"属性(MultipleServicers").
Your code uses the second overload, but specifies a "value" property that doesn't exist ("MultipleServicers").
要解决您的问题,请使用第一个重载:
To fix your problem, either use the first overload:
<%= Html.DropDownList("IsMultipleServicers") %>
或者,将 string MultipleServicers
属性添加到您的视图模型并将其填充到您的控制器中.我推荐这个解决方案,因为它解决了初始显示、发布显示和将发布数据映射到视图/发布模型的几个问题:
Or, add a string MultipleServicers
property 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:
public class ServiceViewModel : ViewModel
{
public string MultipleServicers { get; set; }
public List<SelectListItem> IsMultipleServicers { get; set; }
}
然后对于您的 HTML:
Then for your HTML:
<%= Html.DropDownList(Model.MultipleServicers, Model.IsMultipleServicers) %>
此技术也映射到 MVC2:
This technique maps into MVC2, as well:
<%= Html.DropDownListFor(x => x.MultipleServicers, Model.IsMultipleServicers) %>
这篇关于DropDownList 设置asp.net MVC 中的选定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!