简体   繁体   English

模型类的属性不能设置为null

[英]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.

相关问题 LINQAnonymous类,属性设置为null - LINQAnonymous class, property set to null MVC 3中“模型类”属性中的空值 - Null value in Model Class property in MVC 3 无法将属性设置为字节值,必须将该属性设置为int32类型的非null - Property could not be set to a byte value you must set the property to a non null of type int32 如果引用的模型为NULL,如何将属性值设置为0? - How can I set the property value to 0 if the referenced model is NULL? 当我在模型类中使用导航属性时,Null Reference Exception - Null Reference Exception when i use navigation property in model class 无法将“ tblX”上的属性“ x”设置为“空”值。 您必须将此属性设置为&#39;Int16&#39;类型的非空值 - The property 'x' on 'tblX' could not be set to a 'null' value. You must set this property to a non-null value of type 'Int16' 如果任何属性为空,如何在匿名类中设置默认值; - How to set default value in Anonymous Class if any property is null; 在另一个类中设置属性后,属性似乎重新分配为null - Property seems to reassign to null after I set it in another class Y的X属性不能设置为“十进制”值。 您必须将此属性设置为“ Single”类型的非空值 - The X property on Y could not be set to a 'Decimal' value. You must set this property to a non-null value of type 'Single' 如何摆脱'属性无法设置为double值,必须将此属性设置为decimal类型的非null值' - How to get rid of 'property could not be set to a double value, you must set this property to a non-null value of type decimal'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM