lundi 2 octobre 2017

Using CheckBoxFor with Database First Entity Framework

I've thoroughly searched the site to find what could help me and I've tried each one and I'm still stuck. I built out a SQL Database and created my models from it. I'm making a form and it will have a couple of checkbox fields. The table I have the controller and current views off is

namespace StatsRequestsApp.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;

    public partial class RequestorTable
    {
        public int RequestID { get; set; }

        [Required]
        [DisplayName("Date Of Request")]
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}",
        ApplyFormatInEditMode = true)]
        public System.DateTime RequestDate { get; set; }

        [Required]
        [DisplayName("Date Needed")]
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}",
        ApplyFormatInEditMode = true)]
        public System.DateTime NeededDate { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        [DisplayName("Agency/Program Name")]
        public string AgencyName { get; set; }

        [Required]
        [DisplayName("E-Mail Address")]
        [DataType(DataType.EmailAddress, ErrorMessage = "E-Mail is not valid!")]
        public string EmailAddress { get; set; }

        [Required]
        [DisplayName("Telephone Number")]
        [DisplayFormat(DataFormatString = "{0:###-###-####}")]
        [DataType(DataType.PhoneNumber)]
        public string PhoneNumber { get; set; }

        [DisplayName("Fax Number")]
        [DisplayFormat(DataFormatString = "{0:###-###-####}")]
        [DataType(DataType.PhoneNumber)]
        public string FaxNumber { get; set; }

        public int DetailsID { get; set; }
        public Nullable<int> NotesID { get; set; }

        public virtual DetailsTable DetailsTable { get; set; }
        public virtual NotesTable NotesTable { get; set; }


    }
}`

The table that the foreign key belongs to is

namespace StatsRequestsApp.Models
{
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;

    public partial class DetailsTable
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public DetailsTable()
        {
            this.RequestorTables = new HashSet<RequestorTable>();
        }

        public int DetailsID { get; set; }

        [Required]
        [DisplayName("Project Title")]
        public string ProjectTitle { get; set; }

        [Required, StringLength(250, ErrorMessage = "Please describe in 250 characters or less")]
        [DataType(DataType.MultilineText)]
        [DisplayName("Requested Data")]
        public string RequestedData { get; set; }

        [DisplayName("Type of Analysis")]
        public string AnalysisType { get; set; }
        public int AnalysisID { get; set; }


        [Required]
        [DisplayName("Starting Data Year")]
        [DisplayFormat(DataFormatString = "{0:yyyy}",
        ApplyFormatInEditMode = true)]
        public System.DateTime DataYearStart { get; set; }

        [Required]
        [DisplayName("Ending Data Year")]
        [DisplayFormat(DataFormatString = "{0:yyyy}",
        ApplyFormatInEditMode = true)]
        public System.DateTime DataYearEnd { get; set; }

        public string GeographicArea { get; set; }
        public string PopulationInterest { get; set; }
        public int UseID { get; set; }
        public int ReceiveDataID { get; set; }
        public string Initial { get; set; }

        public virtual AnalysisTable AnalysisTable { get; set; }
        public virtual ReceiveDataTable ReceiveDataTable { get; set; }
        public virtual UseTable UseTable { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", 
               "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<RequestorTable> RequestorTables { get; set; }

        public List<AnalysisTable> analysisList { get; set; }
    }
}

I have the items that will be the Check Boxes in this table

namespace StatsRequestsApp.Models
{
    using System.Collections.Generic;
    using System.ComponentModel;

    public partial class AnalysisTable
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public AnalysisTable()
        {
            this.DetailsTables = new HashSet<DetailsTable>();
        }

        public int AnalysisID { get; set; }

        [DisplayName("Analysis Format")]
        public string AnalysisFormat { get; set; }

        public string OtherAnalysis { get; set; }

        public bool AnalysisSelected { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<DetailsTable> DetailsTables { get; set; }

    }


}

and lastly I have this for statement in my view, which is the only thing that I've been able to link up properly

@for (var i = 0; i < Model.DetailsTable.analysisList.Count; i++)
{
    @Html.CheckBoxFor(model => model.DetailsTable.analysisList[i].AnalysisSelected)
    @Html.HiddenFor(model => model.DetailsTable.analysisList[i].AnalysisID)
    @Html.DisplayFor(model => model.DetailsTable.analysisList[i].AnalysisFormat)
}

The error I receive is System.NullReferenceException: Object reference not set to an instance of an object. Could someone please point out where I'm going at this wrong.

Thank You




Aucun commentaire:

Enregistrer un commentaire