Asp.net mvc 4 无法为mvc中模型中的列表声明赋值
我的模式问题银行\u模式:Asp.net mvc 4 无法为mvc中模型中的列表声明赋值,asp.net-mvc-4,Asp.net Mvc 4,我的模式问题银行\u模式: public List<string> Options { get; set; } 公共列表选项{get;set;} 我的控制器 for (int i = 0; i < Question_IdLIst.Count; i++) { Question_Bank_Model Question = new Question_Bank_Model(); Question.Question = QuestionBal.FetchQu
public List<string> Options { get; set; }
公共列表选项{get;set;}
我的控制器
for (int i = 0; i < Question_IdLIst.Count; i++)
{
Question_Bank_Model Question = new Question_Bank_Model();
Question.Question = QuestionBal.FetchQuestion_ByQuestionID(Question_IdLIst[i]);
List<string> Options_Value = QuestionBal.FetchOption_ByQuestion(Question_IdLIst[i]);
for (int j = 0; j < Options_Value.Count ; j++)
{
Question.Options[j] = Options_Value.ElementAt(j);
}
q.Add(Question);
}
for(int i=0;i
我在该列表中的选项中获得值\u值,但它正在抛出错误
Question.Options[j]=Options\u Value.ElementAt(j)代码>行
错误:对象引用未设置为对象的实例
有人能帮我吗?试试这个
Question.Options = new List<string>();
for (int j = 0; j < Options_Value.Count ; j++)
{
Question.Options.Add(Options_Value.ElementAt(j));
}
Question.Options=newlist();
对于(int j=0;j
谢谢 您忘记初始化选项
问题列表
对象。目前我忘记初始化的地方是空的?具体在哪里初始化取决于你。它可以在类的构造函数中,在属性getter中,或者在你创建了一个问题实例之后在控制器中完成。你能给我看一下代码吗?我没有看到它。我对它印象很深。在new question\u Bank\u Model()之后。
line做一些类似question.Options=new List()的事情代码>。这应该可以