DropDownList的在asp.net MVC设置所选项目 [英] DropDownList setting selected item in asp.net MVC

查看:119
本文介绍了DropDownList的在asp.net MVC设置所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到什么似乎我的错误在asp.net MVC或者干脆我做错了什么。我目前使用的1.0所以也许这是一件将在2.0版本中得到解决。但无论哪种方式,在这里我们去。

当我在我的视图模型有一个属性是相同的名称作为一个下拉列表中宣布的ID,所选择的项目将被忽略,呈现的HTML没有什么选择。
不知道如果我做错了什么,但改变ID的名称解决该问题。我简单的例子,希望它是明确的,否则请让我知道。

这是我的看法,其中宣布ID是相同的名字我在型号列表:

 <表格边框=0的cellpadding =0CELLSPACING =0>
   &所述; TR>
      &所述; TD>
         &所述;%= Html.DropDownList(IsMultipleServicers,Model.IsMultipleServicers)%GT;
      < / TD>
   < / TR>
< /表>

并呈现的HTML

 <表格边框=0的cellpadding =0CELLSPACING =0>
      &所述; TR>
         &所述; TD>
             <选择一个id =IsMultipleServicersNAME =IsMultipleServicers>
                <期权价值=假&GT否LT; /选项>
                <期权价值=真>是< /选项>
             < /选择>
         < / TD>
      < / TR>
< /表>

现在,让我们做一个小的变化。我会改变声明的ID是不同的东西。

下面是我的看法:

 <表格边框=0的cellpadding =0CELLSPACING =0>
    &所述; TR>
       &所述; TD>
          &所述;%= Html.DropDownList(MultipleServicers,Model.IsMultipleServicers)%GT;
       < / TD>
    < / TR>
< /表>

现在呈现的HTML:

 <表格边框=0的cellpadding =0CELLSPACING =0>
   &所述; TR>
      &所述; TD>
         <选择一个id =IsMultipleServicersNAME =IsMultipleServicers>
            <期权价值=假&GT否LT; /选项>
            <选项选择=选择值=真>是< /选项>
         < /选择>
      < / TD>
   < / TR>
< /表>

请注意,现在我得到一个选择的选项这将是该列表中的第二个元素。

这是我的ViewModel只是配合一切融合在一起:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Mvc;命名空间MVCProject.Models.ViewModels.Service
{
    公共类ServiceViewModel:视图模型
    {
         公开名单< SelectListItem> IsMultipleServicers {搞定;组; }
    }
}

这里是我的行动:

 的[AcceptVerbs(HttpVerbs.Get)
公共虚拟的ActionResult服务()
{
   返回查看(新ServiceViewModel()
   {
      IsMultipleServicers = BuildBooleanSelectList(真)
   };
} 私人列表< SelectListItem> BuildBooleanSelectList(布尔IsTrue运算)
 {
    清单< SelectListItem>名单=新名单,LT; SelectListItem>();    如果(IsTrue运算)
    {
       list.Add(新SelectListItem(){选择=假,文本=否,值=假});
       list.Add(新SelectListItem(){选择= TRUE,文本=是,值=真});
    }
    其他
    {
       list.Add(新SelectListItem(){选择= TRUE,文本=否,值=假});
       list.Add(新SelectListItem(){选择=假,文本=是,值=真});
    }
 返回列表;
  }


解决方案

我认为这个问题是关于的DropDownList 重载一个困惑:


  1. Html.DropDownList(字符串名称)查找名称的视图模型属性键入的IEnumerable< SelectListItem> 。它将使用所选的项目( SelectListItem.Selected ==真)从列表中,除非有相同名称的形式岗位价值。


  2. Html.DropDownList(字符串名称,IEnumerable的< SelectListItem>选择列表)使用从选择列表项目,但不是他们选择的值。所选发现通过在视图模型(或交的数据)解决名称和匹配其对 SelectListItem.Value 。即使该值无法找到(或为空),它仍然不会从SelectListItems的列表中使用选定的值。


您code使用第二个过载,但指定不存在(MultipleServicers),价值属性。

要解决您的问题,要么使用第一个重载:

 <%= Html.DropDownList(IsMultipleServicers)%>

或者,添加一个字符串MultipleServicers 属性到您的视图模型,并在控制器中填充它。我建议这个解决方案,因为它得到周围几个问题初始画面,显示后期和后数据映射到一个视图/ post模型:

 公共类ServiceViewModel:视图模型
{
     公共字符串MultipleServicers {搞定;组; }
     公开名单< SelectListItem> IsMultipleServicers {搞定;组; }
}

那么对于你的HTML:

 <%= Html.DropDownList(Model.MultipleServicers,Model.IsMultipleServicers)%GT;

该技术映射到MVC2,以及:

 <%= Html.DropDownListFor(X => x.MultipleServicers,Model.IsMultipleServicers)%GT;

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.

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.

Here is my view where the declared ID is the same name as my list in the model:

<table border="0" cellpadding="0" cellspacing="0">
   <tr>
      <td>
         <%= Html.DropDownList("IsMultipleServicers", Model.IsMultipleServicers) %>
      </td>
   </tr>
</table>

And the rendered 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.

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:

<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:

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;
  }

解决方案

I think the problem is a confusion regarding the DropDownList overloads:

  1. Html.DropDownList(string name) looks for a view model property of name and type IEnumerable<SelectListItem>. It will use the selected item (SelectListItem.Selected == true) from the list, unless there is a form post value of the same name.

  2. 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.

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") %>

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; } 
}

Then for your HTML:

<%= Html.DropDownList(Model.MultipleServicers, Model.IsMultipleServicers) %>

This technique maps into MVC2, as well:

<%= Html.DropDownListFor(x => x.MultipleServicers, Model.IsMultipleServicers) %>

这篇关于DropDownList的在asp.net MVC设置所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆