ASP.NET MVC TASK MANAGER一对多关系

时间:2013-04-08 19:57:24

标签: asp.net-mvc one-to-many relationship

我正在尝试学习ASP.NET MVC所以我已经开始构建小型应用程序 - 基于互联网教程(MVC音乐商店,书呆子晚餐等等)的TASK MANAGER。我在控制器中尝试定义ActionResult方法时遇到困难。

我有模型,我在定义活动。每个活动可以有几个任务。

活动1(每个Activiy都有几个任务(任务1,任务2等)) 活动2 活动3 活动n

问题:

我想请求您帮助定义ActionResult方法,该方法将返回包含与所选活动相关的所有任务列表的视图。

图片来源:http://sdrv.ms/Z46NZE

问题是,我无法将此方法定义为一对多关系。

我的skydrive文件夹中的整个项目:http://sdrv.ms/ZiCbQi

请帮忙。

在我的模型中,我有以下类和Mvcscaffolding生成的Context类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;


namespace TASK3.Models
{
public class Task
{
    public int TaskID { get; set; }
    public string Name { get; set; }
    public int ActivityID { get; set; }
    public DateTime? CreatedOn { get; set; }
    public DateTime? ModifiedOn { get; set; }
    public string PN { get; set; }
    public string Result { get; set; }
    public string ToDo { get; set; }
    public string ResponsiblePerson { get; set; }
    public DateTime? DueDate { get; set; }
    public int? TaskPriority { get; set; }
    public string TaskStatus { get; set; }

    public virtual Activity Activity { get; set; }
}

public class Activity
{
    public int ActivityId { get; set; }
    [Required] public string Name { get; set; }
    public string ActivityType { get; set; }
    public int? ActivityPriority { get; set; }
    public string ActivityStatus { get; set; }

    public virtual ICollection<Task> Tasks { get; set; }
}

}

非常感谢!

最好的问候

Jaroslav Plavec

1 个答案:

答案 0 :(得分:0)

更改

public virtual ICollection<Task> Tasks { get; set; }

public List<Task> Tasks { get; set; }

请记住,一切都必须是可序列化的,ICollection不是,List是。