我怎样才能让这个 ASP.NET MVC SelectList 工作? [英] How can I get this ASP.NET MVC SelectList to work?

查看:42
本文介绍了我怎样才能让这个 ASP.NET MVC SelectList 工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器中创建了一个 selectList,以显示在视图中.

I create a selectList in my controller, to display in the view.

我正在尝试动态创建它,有点像这样......

I'm trying to create it on the fly, sorta thing .. like this...

myViewData.PageOptionsDropDown = 
   new SelectList(new [] {"10", "15", "25", "50", "100", "1000"}, "15");

它可以编译,但输出很糟糕...

It compiles, but the output is bad...

<select id="PageOptionsDropDown" name="PageOptionsDropDown">
    <option>10</option>
    <option>15</option>
    <option>25</option>
    <option>50</option>
    <option>100</option>
    <option>1000</option>
</select>

注意没有选择任何项目?

我该如何解决这个问题??

How can I fix this??

推荐答案

我就是这样做的

IList<Customer> customers = repository.GetAll<Customer>();
IEnumerable<SelectListItem> selectList = 
    from c in customers
    select new SelectListItem
    {
        Selected = (c.CustomerID == invoice.CustomerID),
        Text = c.Name,
        Value = c.CustomerID.ToString()
    };

乍一看我不确定我知道你在追求什么......

At second glance I'm not sure I know what you are after...

这篇关于我怎样才能让这个 ASP.NET MVC SelectList 工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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