使用CheckBoxFor与数据库第一实体框架

时间:2017-10-02 18:02:03

标签: asp.net-mvc entity-framework checkbox

我已经彻底搜索了网站,找到了什么可以帮助我,我已经尝试了每一个,但我仍然卡住了。我构建了一个SQL数据库并从中创建了我的模型。我正在制作表单,它会有几个复选框字段。我关闭控制器和当前视图的表是

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


    }
}`

外键所属的表是

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

我有这个项目中的复选框

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

    }


}

最后我认为这是我的观点,这是我能够正确链接的唯一内容

@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)
}

我收到的错误是

  

System.NullReferenceException:未将对象引用设置为对象的实例。

有人可以指出我在哪里做错了。

0 个答案:

没有答案
相关问题