无法将TuPle列表绑定到Razor中的DropDownList

时间:2013-01-21 15:29:42

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

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

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

错误

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'.

IN RAZOR

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

Class(传递给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;
        }

    }
}

3 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:4)

您可以使用SelectListItem的集合代替使用元组。

在视图模型中添加新属性以获取提醒

public class AddProgramViewModel
{
  //Your other properties here as well

  public string SelectedReminder { set;get;}
  public List<SelectListItem> ReminderList { set;get;}
  public AddProgramViewModel()
  {
     ReminderList=new List<SelectListItem>();
  }
}

现在在您的GET操作方法中,初始化并发送到您的视图。

public ActionResult AddProgram()
{
  var vm=new AddProgramViewModel();

  vm.ReminderList.Add(new SelectListItem { Value="0", Text="None"});
  vm.ReminderList.Add(new SelectListItem { Value="300", Text="5 minutes"});
  //Add more as needed
  //(you may get from DB and add instead of hard coding like this)
}

在视图中,强类型为AddProgramViewModel

@model AddProgramViewModel
@using(Html.Beingform())
{
  @Html.DropDownListFor(x => x.SelectedReminder,
                 new SelectList(Model.RemindersList, "Value", "Text"),"select")
  <input type="submit" />
}

答案 2 :(得分:0)

我有一个生成月份的ID和名称的方法

private List<Tuple<int,string>> generateMonths()
    {
        List<Tuple<int, string>> list = new List<Tuple<int, string>>();
        int[] monthids = new int[12] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
        string[] monthName = new string[12] { "January", "Februray", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
        for (int i = 0; i < 12; i++)
        {
            list.Add(new Tuple<int, string>(monthids[i], monthName[i]));
        }

        return list;
    }

这是绑定我的下拉菜单的方法(asp:dropdownlist) 我的

Item1属性包含月份的ID,而Item2属性包含月份的名称

private void bindMonthsDDL()
        {
            var list=generateMonths();

            if (list.Count > 0)
            {
                ddlMonth.DataSource = list;
                ddlMonth.DataTextField = "Item2";
                ddlMonth.DataValueField = "Item1";
            }
            else
            {
                ddlMonth.DataSource = "";
                
            }
            ddlMonth.DataBind();
        }