M
M
Mr_Virtus2014-06-06 18:57:20
ASP.NET
Mr_Virtus, 2014-06-06 18:57:20

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);
        }

Here is the view for the HttpGet method:
@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>

And here is the HttpPost method
[HttpPost]
        public ActionResult Testing(List<Test> p)
        {
            return View();
        }

How to do it correctly and what could be the problem?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
wkololo_4ever, 2014-06-06
@wkololo_4ever

But, after all, you have not defined fields in the form with model data.

M
Mr_Virtus, 2014-06-06
@Mr_Virtus

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);
        }

and here is the view:
@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>

UserAnswer
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; }
    }
}

D
Deilan, 2015-03-04
@Deilan

@using (Html.BeginForm())
{
    <ol>
        @for(int i = 0; i < Model.Count; i++)
        {
            @Html.EditorFor(m => Model[i].Available);
        }
    </ol>
    <input type="submit" value="Check" />
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question