返回视图时Asp.NET MVC:Controller checklist错误

时间:2018-04-25 16:33:59

标签: c# asp.net-mvc controller

尝试将3个列表返回到视图但是,我一直收到有关将system.generic.list转换为字符串的错误。它接受一个列表但不超过1.最终目标是添加一个清单框,以便我按照我在this website在线找到的教程进行操作。任何帮助将不胜感激

这是控制器:

CronValues cv = new CronValues();

public ActionResult schedulerIndex()
{
    var dayOfMonthList = new List<CronValues>
    {
        new CronValues{DayOfMonth = Convert.ToInt32(Enumerable.Range(1,31).ToList()),IsChecked = false}

    };

    var monthList = new List<CronValues>
    {
        new CronValues{DayOfMonth = Convert.ToInt32(Enum.GetValues(typeof(CronValues.Months))),IsChecked = false}
    };

    var dayOfWeekList = new List<CronValues>
    {
         new CronValues{DayOfWeek = Convert.ToInt32(Enum.GetValues(typeof(CronValues.DaysOfWeek))),IsChecked = false}
    };

    return View(dayOfMonthList, monthList, dayOfWeekList);
}

以下是模型:

public class CronValues
{

   public enum Months
    {
        Jan = 0,
        Feb = 1,
        Mar = 2,
        Apr = 3,
        May = 4,
        Jun = 5,
        Jul = 6,
        Aug = 7,
        Sep = 8,
        Oct = 9,
        Nov = 10,
        Dec = 11
    }

  public  enum DaysOfWeek
    {
        Mon = 0,
        Tue = 1,
        Wed = 2,
        Thu = 3,
        Fri = 4,
        Sat = 5,
        Sun = 6
    }


    public int Second
    {
        get;
        set;
    }

    public int Minute
    {
        get;
        set;
    }

    public int Hour
    {
        get;
        set;
    }

    public int DayOfMonth
    {
        get;
        set;
    }

    public int Month
    {
        get;
        set;
    }

    public int DayOfWeek
    {
        get;
        set;
    }

    public bool IsChecked
    {
        get;
        set;
    }
}

1 个答案:

答案 0 :(得分:1)

您可能希望使用所谓的ViewModel在控制器和视图之间传递复杂数据。

您案例的可能解决方案可能如下所示:

<强> IndexSchedulerViewModel

public class IndexSchedulerViewModel
{
    public List<CronValues> DayOfMonthList { get; set;}
    public List<CronValues> DayOfWeekList { get; set;}
    public List<CronValues> MonthList { get; set;}
}

<强> SchedulerController

CronValues cv = new CronValues();

// GET: Scheduler
[HttpGet]
public ActionResult schedulerIndex()
{
    var dayOfMonthList = new List<CronValues>
    {
        new CronValues{DayOfMonth = Convert.ToInt32(Enumerable.Range(1,31).ToList()),IsChecked = false}

    };

    var dayOfWeekList = new List<CronValues>
    {
        new CronValues{DayOfWeek = Convert.ToInt32(Enum.GetValues(typeof(CronValues.DaysOfWeek))),IsChecked = false}
    };

    var monthList = new List<CronValues>
    {
        new CronValues{DayOfMonth = Convert.ToInt32(Enum.GetValues(typeof(CronValues.Months))),IsChecked = false}
    };

    return View(new IndexSchedulerViewModel
    {
        DayOfMonthList = dayOfMonthList,
        DayOfWeekList = dayOfWeekList,
        MonthList = monthList
    });
}

索引视图

@model YourProjectNamespace.ViewModels.IndexSchedulerViewModel

// The code below demonstrates how to access the ViewModel's properties on the view.
@Model.DayOfMonthList
@Model.DayOfWeekList
@Model.MonthList