Asp.net mvc 在mvc中将元素添加到列表中时出现的问题

Asp.net mvc 在mvc中将元素添加到列表中时出现的问题,asp.net-mvc,asp.net-mvc-4,razor,Asp.net Mvc,Asp.net Mvc 4,Razor,我正在尝试创建一个在线考试系统。这里,在我的控制器中,我从Razor视图中获取一个问题对象,包括选定的答案,我需要将这些答案添加到列表中。 这是我的控制器方法: [HttpPost] public ActionResult Index(QuestionLoadDTO ques) { List<string> answersList = new List<string>(); int count = 0; count = int.Parse(Requ

我正在尝试创建一个在线考试系统。这里,在我的控制器中,我从Razor视图中获取一个问题对象,包括选定的答案,我需要将这些答案添加到列表中。 这是我的控制器方法:

[HttpPost]
public ActionResult Index(QuestionLoadDTO ques)
{
    List<string> answersList = new List<string>();
    int count = 0;
    count = int.Parse(Request["qid"].ToString());
    count++;
    string selectedAnswer = ques.selected.ToString();
    answersList.Add(selectedAnswer);
    if (count <= 4)
    {
        IQuestionService ser = new QuestionService();
        QuestionLoadDTO q = ser.GetIndividualQuestions(count);
        return View(q);
    }
    Session["msg"] = answersList;
    return RedirectToAction("Submit");
}  
提交Razor视图:

@{
    //ViewBag.Title = "Submit";
    var list= Session["msg"] as IEnumerable<string>;
}

<h2>Submit</h2>
<div>Submit</div>

@foreach (var item in list)
{
    <div>@item</div>
}
@{
//ViewBag.Title=“提交”;
变量列表=会话[“msg”]作为IEnumerable;
}
提交
提交
@foreach(列表中的变量项)
{
@项目
}
我无法在中添加所有答案元素。它只添加最后的答案元素。
(目前我的数据库中只有4个问题)

您正在为每个帖子请求创建一个新的
应答列表。您需要创建一次列表并将其存储在
会话中

[HttpPost]
public ActionResult Index(QuestionLoadDTO ques)
{
    List<string> answersList = null;
    if(Session["answersList"] != null)
        answersList = Session["answersList"] as List<string>;
    else 
    {   
        answersList = new List<string>();
        Session["answersList"] = answersList;
    }
    int count = 0;
    count = int.Parse(Request["qid"].ToString());
    count++;
    string selectedAnswer = ques.selected.ToString();
    answersList.Add(selectedAnswer);
    if (count <= 4)
    {
        IQuestionService ser = new QuestionService();
        QuestionLoadDTO q = ser.GetIndividualQuestions(count);
        return View(q);
    }
    Session["msg"] = answersList;
    return RedirectToAction("Submit");
}
[HttpPost]
公共行动结果索引(问题加载到问题)
{
列表应答列表=null;
如果(会话[“应答列表”]!=null)
应答列表=会话[“应答列表”]作为列表;
其他的
{   
answersList=新列表();
会话[“应答列表”]=应答列表;
}
整数计数=0;
count=int.Parse(请求[“qid”].ToString());
计数++;
字符串selectedAnswer=ques.selected.ToString();
应答列表。添加(选择应答);

if(count@Dayan,对不起,做得太匆忙了。您需要第一次将列表存储在会话中。现在检查代码。
[HttpPost]
public ActionResult Index(QuestionLoadDTO ques)
{
    List<string> answersList = null;
    if(Session["answersList"] != null)
        answersList = Session["answersList"] as List<string>;
    else 
    {   
        answersList = new List<string>();
        Session["answersList"] = answersList;
    }
    int count = 0;
    count = int.Parse(Request["qid"].ToString());
    count++;
    string selectedAnswer = ques.selected.ToString();
    answersList.Add(selectedAnswer);
    if (count <= 4)
    {
        IQuestionService ser = new QuestionService();
        QuestionLoadDTO q = ser.GetIndividualQuestions(count);
        return View(q);
    }
    Session["msg"] = answersList;
    return RedirectToAction("Submit");
}