Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我试图在表上实现crud操作,在表中我从dropdownlist中选择FK数据。当我尝试保存时,会出现NullReference异常_C#_Asp.net_Entity Framework_Model - Fatal编程技术网

C# 我试图在表上实现crud操作,在表中我从dropdownlist中选择FK数据。当我尝试保存时,会出现NullReference异常

C# 我试图在表上实现crud操作,在表中我从dropdownlist中选择FK数据。当我尝试保存时,会出现NullReference异常,c#,asp.net,entity-framework,model,C#,Asp.net,Entity Framework,Model,我的模型是: public class Presentation { public int PresentationId { get; set; } public string PresentationTitle { get; set; } public DateTime PresentationTime { get; set; } public Level? Level { get; set; } public virtual Professor Pro

我的模型是:

public class Presentation
{
    public int PresentationId { get; set; }
    public string PresentationTitle { get; set; }
    public DateTime PresentationTime { get; set; }
    public Level? Level { get; set; }
    public virtual Professor Professor { get; set; }
}

public class Professor
{
    public int ProfessorId { get; set; }
    public string Name { get; set; }
    public DateTime EnrollmentDate { get; set; }
    public virtual ICollection<Presentation> Presentations { get; set; }
}
DropDownList用名称填充,但在保存操作时,我得到一个NullReference异常。保存看起来像这样:

Presentation newPres = new Presentation();
newPres.PresentationTitle = txtPresentationTitle.Text;
newPres.Level = ParseEnum<Level>(drpLevel.SelectedValue);
newPres.PresentationTime = calendar.SelectedDate;
newPres.Professor.ProfessorId = Convert.ToInt32(drpProfessor.SelectedItem.Value);

db.Presentations.Add(newPres);
db.Entry(newPres).State = System.Data.EntityState.Added;

db.SaveChanges();
litMsg.Text = "New presentation added!";
我试图将所选项目的ProfessorId值添加到db中,但我得到:

对象引用未设置为对象的实例


我选中了,valueProfessorId是从下拉列表中选择的。

最有可能的newPres.ProfessorId的可能副本为NULL。在调试器中查看它,它将准确地告诉您哪个变量为NULL。我们只能猜测。从演示文稿的初始化中,我看不出在设置ProfessorId之前Professor可以先初始化到哪里。您的最佳选择是在课堂演示文稿中包含外键,并直接设置ProfessorId,而无需通过Professor访问。请尝试drpProfessor.SelectedValue而不是drpProfessor.SelectedItem.Value。我不确定。。。也许会有帮助。。。
Presentation newPres = new Presentation();
newPres.PresentationTitle = txtPresentationTitle.Text;
newPres.Level = ParseEnum<Level>(drpLevel.SelectedValue);
newPres.PresentationTime = calendar.SelectedDate;
newPres.Professor.ProfessorId = Convert.ToInt32(drpProfessor.SelectedItem.Value);

db.Presentations.Add(newPres);
db.Entry(newPres).State = System.Data.EntityState.Added;

db.SaveChanges();
litMsg.Text = "New presentation added!";
newPres.Professor.ProfessorId = Convert.ToInt32(drpProfessor.SelectedItem.Value);