在MVC3创建下拉列表 [英] Creating Dropdown List in MVC3

查看:200
本文介绍了在MVC3创建下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个下拉列表中显示的自定义集合类的所有值

 公共类MyCustomClassCollection
{公开名单< MyCustomClass> {获取;设置;}}

我想让它显示了说明:每个MyCustomClass字符串

我试过

 <%:&GT Html.DropDownList说明,MyCustomClass%;

ReSharper的建议我投MyCustomClass到IEnemerable

但服务器返回一个无法施放的错误。

任何想法我怎么能创造这个DropDownList的?

的__ 修改 _ __

 公共类视图模型
    {
        公众详细细节{搞定;组; }
    }公共类细节//从webservce继承
{
   公共CustomClassCollection {获取;设置;}
   ....其他属性,a.k.a自定义类别
}
公共类CustomClassCollection
{
   公开名单< CustomClass> {获取;设置;}
}公共类CustomClass {
  公众诠释标识{获取;设置;}
  公共字符串描述{获取;设置;}
  ...其他属性}
公众的ActionResult指数(INT?编号,日期时间?日)
        {
            如果(id.Equals(空))
                ID = #########;
            如果(date.Equals(空))
                日期= DateTime.Today;
            VAR VM =新的ViewModel
                         {
                             详细= _repository.Detail((INT)ID,(日期时间)为准)
                         };
            返回查看(VM);
        }


解决方案

的DropDownList 助手的第二个参数必须是一个的IEnumerable< SelectListItem> 的SelectList 实现此接口为此事。因此,在你的控制器动作组织在您将您的自定义收集到这样的IEnumerable< SelectListItem> 。与往常一样,你可以通过编写一个视图模型启动:

 公共类MyViewModel
{
    公共字符串SelectedDescription {搞定;组; }
    公众的SelectList说明{搞定;组; }
}

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

 公众的ActionResult指数()
{
    VAR描述= yourCustomCollection.MyCustomClass.Select(X =>新建
    {
        值= x.Description,
        文字= x.Description
    });
    VAR模型=新MyViewModel
    {
        说明=新的SelectList(描述中,价值,文本)
    };
    返回查看(模型);
}

终于在你的强类型的视图:

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


更新:

发布更新的模型(其中的方式仍然是不完整的,也不可能编译因为你没有提供任何属性名称)后,这里是一个例子:

 公共类视图模型
{
    公众诠释SelectedId {搞定;组; }
    公众详细细节{搞定;组; }
}公共类细节
{
   公共CustomClassCollection MyCollection的{搞定;组; }
}
公共类CustomClassCollection
{
   公开名单< CustomClass> CustomClass {搞定;组; }
}公共类CustomClass
{
    公众诠释标识{搞定;组; }
    公共字符串描述{搞定;组; }
}公共类HomeController的:控制器
{
    公众的ActionResult指数()
    {
        VAR VM =新的ViewModel
        {
            细节=新的细节
            {
                MyCollection的=新CustomClassCollection
                {
                    CustomClass =新的List< CustomClass>
                    {
                        新CustomClass
                        {
                            ID = 1,
                            描述=说明1
                        },
                        新CustomClass
                        {
                            ID = 2,
                            描述=描述2
                        },
                        新CustomClass
                        {
                            ID = 3,
                            描述=说明3
                        },
                    }
                }
            }
        };
        返回查看(VM);
    }
}

和视图:

 <%= Html.DropDownListFor(
    X => x.SelectedId,
    新的SelectList(Model.Detail.MyCollection.CustomClass,ID,说明)
)%GT;

你有什么才能定义ASP.NET MVC IUS一个下拉列表,你需要两件事情就明白了:


  1. 要(在我的例子 SelectedId )选择的值绑定一个标量属性

  2. 要在列表中绑定( Model.Detail.MyCollection.CustomClass 中的例子)集合

I am trying to create a dropdown list to display all the value in a custom collection class such as

public class MyCustomClassCollection
{

public List<MyCustomClass> {get;set;}

}

I want it to show the Description:string of each MyCustomClass

I tried

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

Resharper suggests that I cast MyCustomClass to IEnemerable

but the server returns an unable to cast error.

Any Idea how I can create this DropDownList?

__Modification___

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

解决方案

The second argument of the DropDownList helper must be an IEnumerable<SelectListItem> or a SelectList which implements this interface for that matter. So in your controller action organize in such a way that you convert your custom collection into an IEnumerable<SelectListItem>. As always you could start by writing a view model:

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

and then have your controller action query the custom list and populate the view model which will be passed to the view:

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

and finally in your strongly typed view:

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


UPDATE:

After posting your updated models (which by the way are still incomplete and impossible to compile as you haven't provided any property names), here's an example:

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

and in the view:

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

What you have to understand in order to define a dropdown list in ASP.NET MVC ius that you need 2 things:

  1. A scalar property to bind the selected value to (SelectedId in my example)
  2. A collection to bind the list to (Model.Detail.MyCollection.CustomClass in the example)

这篇关于在MVC3创建下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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