自定义POST操作包括子元素

时间:2015-02-04 10:44:49

标签: javascript c# json asp.net-mvc collections

我想替换我的模型类CourseEvent的默认创建视图和控制器,它应该能够处理包含子元素的请求。子元素的类型为public virtual ICollection<CourseEventDate>

模型类CourseEvent

public partial class CourseEvent
{
    public CourseEvent()
    {
        this.CourseEventDates = new HashSet<CourseEventDate>();
        this.UsersInCourseEvents = new HashSet<UsersInCourseEvent>();
    }

    public int CourseEventId { get; set; }
    public Nullable<int> CourseFk { get; set; }
    public Nullable<int> CourseEventDurationInDays { get; set; }
    public Nullable<int> CourseEventDurationInHours { get; set; }
    public Nullable<int> CourseEventPrice { get; set; }

    public virtual Course Course { get; set; }
    public virtual ICollection<CourseEventDate> CourseEventDates { get; set; }
    public virtual ICollection<UsersInCourseEvent> UsersInCourseEvents { get; set; }
}

模型类CourseEventDate

public partial class CourseEventDate
{
    public int CourseEventDateId { get; set; }
    public Nullable<int> CourseEventFk { get; set; }
    public Nullable<System.DateTime> CourseEventDateTimeFrom { get; set; }
    public Nullable<System.DateTime> CourseEventDateTimeTo { get; set; }

    public virtual CourseEvent CourseEvent { get; set; }
}

如果mvc将jSON用于脚踏式POST请求,我会考虑使用JavaScript发送POST请求,JSON请求看起来像这样:

{
    "CourseFk": "123",
    "CourseEventDurationInDays": "2",
    "CourseEventDurationInHours": "16",
    "CourseEventPrice": "1234",
    "CourseEventDate": [{
         "CourseEventDateTimeFrom": "2015-05-05 08:00",
         "CourseEventDateTimeTo": "2015-05-05 12:00"
     },
     {
         "CourseEventDateTimeFrom": "2015-05-05 13:00",
         "CourseEventDateTimeTo": "2015-05-05 17:00"
     },
     {
         "CourseEventDateTimeFrom": "2015-05-06 08:00",
         "CourseEventDateTimeTo": "2015-05-06 12:00"
     },
     {
         "CourseEventDateTimeFrom": "2015-05-06 13:00",
         "CourseEventDateTimeTo": "2015-05-06 17:00"
     }]

}

那么解决这个问题的最佳策略/最佳实践是什么?

0 个答案:

没有答案