在MVC3中创建下拉列表

时间:2011-09-18 15:23:25

标签: asp.net-mvc-3 c#-4.0 html-helper

我正在尝试创建一个下拉列表来显示自定义集合类中的所有值 比如

public class MyCustomClassCollection
{

public List<MyCustomClass> {get;set;}

}

我希望它显示每个MyCustomClass的描述:字符串

我试过

<%: Html.DropDownList "Description", MyCustomClass %>

Resharper建议我将MyCustomClass转换为IEnemerable

但服务器返回无法转换错误。

任何想法我如何创建这个DropDownList?

__ 修改 _ __ < / p>

public class ViewModel
    {
        public Detail detail { get; set; }
    }

public class Detail   //Inherited from webservce
{
   public CustomClassCollection {get;set;}
   .... Other Properties, a.k.a Custom Classes
}


public class CustomClassCollection
{
   public List<CustomClass> {get;set;}
}

public class CustomClass {
  public int Id {get;set;}
  public string Description{get;set;}
  ... other properties

}


public ActionResult Index(int? id, DateTime? date)
        {
            if (id.Equals(null))
                id = ######### ;
            if (date.Equals(null))
                date = DateTime.Today;
            var vm = new ViewModel
                         {
                             Detail = _repository.Detail((int)id,(DateTime)date)
                         };
            return View(vm);
        }

1 个答案:

答案 0 :(得分:3)

DropDownList帮助器的第二个参数必须是IEnumerable<SelectListItem>SelectList,它实现了此接口。因此,在您的控制器操作中,您可以将自定义集合转换为IEnumerable<SelectListItem>。与往常一样,您可以从编写视图模型开始:

public class MyViewModel
{
    public string SelectedDescription { get; set; }
    public SelectList Descriptions { get; set; }
}

然后让您的控制器操作查询自定义列表并填充将传递给视图的视图模型:

public ActionResult Index()
{
    var descriptions = yourCustomCollection.MyCustomClass.Select(x => new
    {
        Value = x.Description,
        Text = x.Description
    });
    var model = new MyViewModel
    {
        Descriptions = new SelectList(descriptions, "Value", "Text")
    };
    return View(model);
}

最后在你的强类型视图中:

<%= Html.DropDownListFor(x => x.SelectedDescription, Model.Decriptions) %>

更新:

在发布您更新的模型后(顺便说一下,由于您没有提供任何属性名称,这些模型仍然不完整且无法编译),以下是一个示例:

public class ViewModel
{
    public int SelectedId { get; set; }
    public Detail Detail { get; set; }
}

public class Detail
{
   public CustomClassCollection MyCollection { get; set; }
}


public class CustomClassCollection
{
   public List<CustomClass> CustomClass { get; set; }
}

public class CustomClass 
{
    public int Id { get; set; }
    public string Description { get; set; }
}

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var vm = new ViewModel
        {
            Detail = new Detail
            {
                MyCollection = new CustomClassCollection
                {
                    CustomClass = new List<CustomClass>
                    {
                        new CustomClass
                        {
                            Id = 1,
                            Description = "description 1",
                        },
                        new CustomClass
                        {
                            Id = 2,
                            Description = "description 2",
                        },
                        new CustomClass
                        {
                            Id = 3,
                            Description = "description 3",
                        },
                    }
                }
            }
        };
        return View(vm);
    }
}

并在视图中:

<%= Html.DropDownListFor(
    x => x.SelectedId, 
    new SelectList(Model.Detail.MyCollection.CustomClass, "Id", "Description")
) %>

为了在ASP.NET MVC ius中定义下拉列表,您需要了解以下内容:

  1. 将所选值绑定到(我的示例中为SelectedId)的标量属性
  2. 用于将列表绑定到(示例中的Model.Detail.MyCollection.CustomClass)的集合