dimanche 28 juin 2015

MVC, Uploading Models containing a list of another model and additional Properties

I have a the following classes

public class Course
  {
    [Required]
    public string Name { get; set; }  
    public virtual ICollection<Occasion> Occasions { get; set; } 
  }
 public class Occasion
  { 
    [ForeignKey("CourseId")]
    public Course Course{ get; set; }
    public int CourseId { get; set; } 
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
  }

and view Models

public class CourseModel
  {
    [Required]
    public string Name { get; set; } 
    public List<OccasionModel> Occasions { get; set; } 
  }
public class OccasionModel
  { 
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
  }

My intention is to have a view where I can create/edit a new course with one or several occasions. I want have just one line where I write an occasion for a course, and a new line that shows up only if I choose to add a new occasion. Understandably, the size of the list is determied by the user.

I have tried using partial pages as well as using helper Html.EditorFor along with a template view page for occasionModel in Shared folder in the similar fashion as one would generate a checkboxes for a list in a model. I did google a lot but the questions are either unanswered or in other languages. It is probably possible to make a page where one creates the course without occasions and then using a partial page edits the course so that it adds new occasions. Isn't there an easy way of creating both the course and its occasions in one go? Thanks

Aucun commentaire:

Enregistrer un commentaire