C# Razor DropDownList对于SelectList,如何绑定不可数selectitems或如何绑定?

C# Razor DropDownList对于SelectList,如何绑定不可数selectitems或如何绑定?,c#,razor,C#,Razor,下面的代码工作正常,我在许多视图中使用这种绑定方法,因此我尝试将其转换为静态函数。以下是我的尝试。有人能帮我强调一下如何做到这一点吗 @Html.DropDownListFor(model => model.CardExpiryYear, new SelectList( new[] { new { Text = "- -", Value = (string)null } }.Concat( Enumerable.Range(DateTime.Now.Year, 10)

下面的代码工作正常,我在许多视图中使用这种绑定方法,因此我尝试将其转换为静态函数。以下是我的尝试。有人能帮我强调一下如何做到这一点吗

@Html.DropDownListFor(model => model.CardExpiryYear, new SelectList(
    new[] { new { Text = "- -", Value = (string)null } }.Concat(
    Enumerable.Range(DateTime.Now.Year, 10)
    .Select(r => new
    {
        Text = r.ToString(),
        Value = r.ToString()
    })),
    "Value", "Text")
)
以下是我的尝试:

@Html.DropDownListFor(model => model.CardExpiryYear, IEnumerable < SelectListItem > vmConstants.listExpiryYearTest)

public static SelectList listExpiryYearTest()
        {
            var listExpYears = new SelectList(
                        new[] { new { Text = "- -", Value = (string)null } }.Concat(
                        Enumerable.Range(DateTime.Now.Year, 10)
                        .Select(r => new
                        {
                            Text = r.ToString(),
                            Value = r.ToString()
                        })),
                        "Value", "Text");
            return listExpiryYears; 
        }
此代码看起来是错误的:

@Html.DropDownListFor(model => model.CardExpiryYear, 
    IEnumerable < SelectListItem > vmConstants.listExpiryYearTest)
这假设您有一个名为vmConstants的类,该类具有一个名为listexpiryearyest的静态方法


旁注:考虑使用标准C,它将类和方法名大写。

是否只有共享的内部选择列表,或者是否有许多带有CARDestyYYLY属性的模型?有一个称为支付的模型,它的一个属性是公共字符串CARDXYPIYYYEAR {GET;SET;},我在一个单独的类中维护了我所有的常量,我想在这个类中创建一个可以返回年份列表的函数。我已附加了前面尝试过的函数。使用泛型类型“System.Collections.generic.IEnumerable”需要1个类型参数
@Html.DropDownListFor(m => m.CardYearExpiry, (IEnumerable<SelectListItem>) ViewBag.CardYearExpiry,  null)
@Html.DropDownListFor(m => m.CardYearExpiry, (IEnumerable<SelectListItem>) ViewBag.CardYearExpiry,  null)
ViewBag.CardYearExpiry = item.Select(s => new SelectListItem
{
  Value = s.Id,
  Text = s.Name
}).OrderBy(o => o.Text);