Answer the question
In order to leave comments, you need to log in
How to pass collection from [HttpGet] method to view, and then filled in [HttpPots] method?
Hello.
Please tell me,
I pass the collection to the view and then return to the [HttpPost] method, but the p parameter of this method is null
Here is HttpGet
[HttpGet]
public ActionResult Testing()
{
List<Test> t = new List<Test>();
return View(t);
}
@model List<Questionnaire.Models.Test>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Testing</title>
</head>
<body>
@using (Html.BeginForm()){
<div>
</div>
<input type="submit" value="ok" />
}
</body>
</html>
[HttpPost]
public ActionResult Testing(List<Test> p)
{
return View();
}
Answer the question
In order to leave comments, you need to log in
But, after all, you have not defined fields in the form with model data.
Defined. I just didn't post all the code.
Here is the get:
[HttpGet]
public ActionResult Testing(int id)
{
List<UserAnswer> userAnswers = new List<UserAnswer>();
var questions = (from question in db.Questions
where question.TestID == id
select question).ToList();
foreach (var questionItem in questions)
{
var correctAnswers = questionItem.Answer.Split(';');
foreach (var correctAnswer in correctAnswers)
{
userAnswers.Add(new UserAnswer{QuestionText = questionItem.Text, Answer = correctAnswer, Available = false, QuestionType = questionItem.Type});
}
}
return View(userAnswers);
}
@model List<Questionnaire.Models.UserAnswer>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Testing</title>
</head>
<body>
@using (Html.BeginForm()){
<ol>
@foreach (var item in Model)
{
@Html.CheckBoxFor(m => item.Available);
}
}
</ol>
<input type="submit" value="Check" />
}
</body>
</html>
namespace Questionnaire.Models
{
public class UserAnswer
{
public string QuestionText { get; set; }
public string QuestionType{get; set;}
public string Answer { get; set; }
public bool Available { get; set; }
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question