c# - 无法将 TuPle 列表绑定(bind)到 Razor 中的 DropDownList

标签 c# asp.net-mvc asp.net-mvc-3 razor

在 MVC 3 中,我需要将 DropDownList 绑定(bind)到元组...我收到以下错误:

你能告诉我这里做错了什么吗?谢谢

错误

DataBinding: 'System.Tuple`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'Value'.

在 Razor 里

@Html.DropDownListFor(
    x => x.RemindersList,
    new SelectList(Model.RemindersList, "Value", "Text")
)

类(传递给 ViewModel 的实例)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AshtonEventsMobile
{
    public class Reminders
    {
        public List<Tuple<int, string>> TimeSpanText { get; set; }


        // We are setting the default values using the Costructor
        public Reminders()
        {
            List<Tuple<int, string>> timeSpanText = new List<Tuple<int, string>>();

            Tuple<int, string> reminder0 = new Tuple<int, string>(0, "None");
            timeSpanText.Add(reminder0);

            Tuple<int, string> reminder1 = new Tuple<int, string>(300, "5 minutes before");
            timeSpanText.Add(reminder1);

            Tuple<int, string> reminder2 = new Tuple<int, string>(900, "15 minutes before");
            timeSpanText.Add(reminder2);

            Tuple<int, string> reminder3 = new Tuple<int, string>(1800, "30 minutes before");
            timeSpanText.Add(reminder3);

            Tuple<int, string> reminder4 = new Tuple<int, string>(3600, "1 hour before");
            timeSpanText.Add(reminder4);

            Tuple<int, string> reminder5 = new Tuple<int, string>(7200, "2 hours before");
            timeSpanText.Add(reminder5);

            Tuple<int, string> reminder6 = new Tuple<int, string>(86400, "1 day before");
            timeSpanText.Add(reminder6);

            Tuple<int, string> reminder7 = new Tuple<int, string>(172800, "2 day before");
            timeSpanText.Add(reminder7);

            TimeSpanText = timeSpanText;
        }

    }
}

最佳答案

错误总结 - 它确实没有名为 Value 的属性. Tuple<T1, T2> 类型的元组具有属性 Item1Item2 - 可以在 MSDN 上找到更多信息 here .

关于c# - 无法将 TuPle 列表绑定(bind)到 Razor 中的 DropDownList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14441924/

相关文章:

c# MVC 5 RouteConfig 重定向

c# - C# 中的继承与接口(interface)

c# - Dictionary<X, Func<Y>> 从其中的 Func<> 访问包含的 Dictionary

c# - 如何在动态 html 表的 requirefield 验证器中显示动态。

asp.net-mvc-3 - 从客户端检测到潜在危险的 Request.Path 值 (:)

c# - 将 ASP.NET MVC 模型封装为可重用程序集

asp.net - int 溢出时本地化为 "the value {0} is invalid"

c# - 在 Windows 7 中分析文件的碎片

asp.net - 在 ASP.Net Core Identity 中获取当前主体作为我的自定义应用程序用户

asp.net-mvc - SignalR 客户端在每次刷新页面时断开/连接。 ASP.NET MVC