I have an Entity Class called Report. Associated with it I have three HashSets of other classes, ReportRecipients, ReportAuthors and AuthorizedUsers. The three HashSets define many to many relations between the Report and the Recipient, Author and User tables.
In my Razor, I have:
@for (int i = 0; i < Model.ReportRecipients.Count; i++)
{
@Html.HiddenFor(model => model.ReportRecipients.ElementAt(i).ReportRecipient.ID)
@Html.CheckBoxFor(model => model.ReportRecipients.ElementAt(i).ReportShared)
@Html.TextBoxFor(model => model.ReportRecipients.ElementAt(i).DueDate)
@Html.HiddenFor(model => model.ReportRecipients.ElementAt(i).ReportRecipient.Email)
@Html.HiddenFor(model => model.ReportRecipients.ElementAt(i).ReportRecipient.Phone)
...
}
My report class looks like:
[Table("Report")]
public partial class Report
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Report()
{
AuditLogs = new HashSet<AuditLog>();
AuthorizedUsers = new HashSet<AuthorizedUser>();
ReportRecipients= new HashSet<ReportRecipient>();
ReportAuthors= new HashSet<ReportAuthor>();
}
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long ID { get; set; }
[Key]
[StringLength(15)]
[DisplayName("Report Number")]
public string ReportNumber { get; set; }
...
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AuditLog> AuditLogs { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AuthorizedUser> AuthorizedUsers{ get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ReportAuthor> ReportAuthors{ get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ReportRecipient> ReportRecipients{ get; set; }
}
However, when I submit my form, I'm either getting null values for the navigation properties or a null value exception later in my code.
What am I doing wrong with my loops? IF I can't make this work with MVC, I'll have to rework the entire form in AngularJS.
Thanks.