1

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

剃刀

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

    }
}
4

3 回答 3

5

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

于 2013-01-21T15:32:14.937 回答
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" />
}
于 2013-01-21T15:46:56.580 回答
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();
        }
于 2020-06-24T06:28:39.930 回答