[英]Property on model class could not be set to null
My application is basically just a survey with questions and multiple choice answers. 我的应用程序基本上只是一个包含问题和多项选择答案的调查。 Not all of the questions are intended to be asked of everyone, but only when a person selects a certain answer to a previous question. 并非所有人都希望提出所有问题,只有当一个人选择了上一个问题的某个答案时,才会提出这个问题。 ie They select C on question 15, then question 15.5 will need to be asked, otherwise question 16 is asked. 即,他们在问题15上选择C,然后需要询问问题15.5,否则将询问问题16。 I am just adding this branching, so everything else has been working. 我只是添加此分支,所以其他所有功能都在工作。
I added an int property "DependentAnswer" to the "Question" class to hold the ID of an answer which causes the given question to be asked. 我在“问题”类中添加了一个int属性“ DependentAnswer”,以保存答案的ID,该ID导致询问给定的问题。 One answer may cause multiple questions, but the reverse is not true. 一个答案可能会引起多个问题,但事实并非如此。
When I add to the "Answer" class a property "DependentQuestions" which is a list of int meant to hold the ids of any questions which should be asked if the given answer is chosen, I get this error: The 'DependentAnswer' property on 'Question' could not be set to a 'null' value. 当我在“ Answer”类中添加一个属性“ DependentQuestions”,该属性是一个int列表,用于保存是否应该选择给定答案的任何问题的ID,我会收到以下错误消息:“ DependentAnswer”属性无法将“问题”设置为“空”值。 You must set this property to a non-null value of type 'System.Int32'. 您必须将此属性设置为'System.Int32'类型的非空值。
Here is the important stuff of my question class: 这是我的问题课的重要内容:
public class Question
{
[Key]
public int id { get; set; }
public string question { get; set; }
public int DependentAnswer { get; set; }
}
And here is my Answer class. 这是我的答案班。 I created a constructor which I meant to populate the DependentQuestions list, but I'm getting an error which seems to have something to do with the LINQ and the fact that DependentAnswer is null in any question which isn't dependent on a, but I'm not experienced enough to know what's wrong. 我创建了一个旨在填充DependentQuestions列表的构造函数,但出现错误,这似乎与LINQ有关,并且在任何不依赖于的问题中DependentAnswer为null,但我没有足够的经验来知道出了什么问题。
public class Answer
{
[Key]
public int id { get; set; }
[Required]
public string answer { get; set; }
[Required]//which question does this answer belong to
public int questionId { get; set; }
public List<int> DependentQuestions { get; set; }
public Answer()
{
using (dbSurvey db = new dbSurvey())
{
var _list = db.Questions.Where(q => q.DependentAnswer == id).Select(q => q.id).ToList();
if (_list.Any())
{
DependentQuestions = _list;
}
else
{
DependentQuestions.Add(0);
}
}
}
}
尝试使DependentAnswer属性为空
public int? DependentAnswer { get; set; }
First initialize your list on your constructor so that it will not trigger an error: 首先在构造函数上初始化您的列表,以使其不会触发错误:
public Answer()
{
DependentQuestions = new List<int>();
//Your additional logic here
}
Then if you have null int when using your Question class, then make it nullable: 然后,如果在使用Question类时具有int值,则将其设置为可空值:
public int? DependentAnswer { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.