0

I want to replace the default Create View and Controller for my modelclass CourseEvent which should be able to handle request containing child elements. The child elements are of type public virtual ICollection<CourseEventDate>.

Model Class 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; }
}

Model Class 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; }
}

If mvc would use JSON for the scafffolded POST requests, I would've considered sending a POST request using JavaScript, the JSON request would look something like this:

{
    "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"
     }]

}

So what's the best strategy/best practices to solve this problem?

Ronin
  • 7,322
  • 6
  • 36
  • 54
  • What is your actual question? What problem are your having? –  Feb 04 '15 at 10:54
  • I want to be able create a `CourseEvent` and some child elements of type `CourseEventDate`s from the same view. – Ronin Feb 04 '15 at 11:06
  • You haven't even shown your view or the method your posting back to, but assuming you want to dynamically add (and/or remove) `CourseEventDates` in the view [this answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) shows some approaches you can consider –  Feb 04 '15 at 11:10

0 Answers0