0

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.

  • 1
    You cannot use `.ElementAt()`. Change your inputs to use `@Html.HiddenFor(m => m.ReportRecipients[i].ReportRecipient.ID)` etc. Refer also [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) and compare the html your generating by using `.ElementAt()` Note that you collections need to implement `IList`, otherwise you need an `EditorTemplate` (and do not use data models in your view - create view models) –  Feb 08 '17 at 20:46
  • Can you post the controller code that you're using to pass data to your view? Have you tried debugging the controller to make sure your EF code is populating navigation properties correctly and not lazily leaving them out? – Darendal Feb 08 '17 at 20:47
  • @StephenMuecke, Thanks, that link was exactly what I needed. I'll implement my fix. If you can post an answer with this info, I'll accept it and up vote it. Also, This was a project I just inherited today from another developer. I'll be replacing all of the domain models with view models shortly. I just need to get the form to work first. –  Feb 08 '17 at 21:04
  • 1
    I duped it instead :) –  Feb 08 '17 at 21:06
  • @Darendal, Thanks for the comment. The navigation properties are loading fine. I was able to display them on the form for existing reports. It was just a deficiency in my ASP.Net knowledge. I need to forget all of my Grails knowledge. –  Feb 08 '17 at 21:07

0 Answers0