Это не сильно напечатано.
Вот твоя самая большая проблема. Так сильно набери ...
... посмотреть модель конечно:
public class AnswerViewModel
{
public string Label { get; set; }
public string Value { get; set; }
}
public class QuestionViewModel
{
public string Title { get; set; }
public string Answer { get; set; }
public IEnumerable<string> PossibleAnswers { get; set; }
}
Затем напишите контролеру, который будет отвечать за показ формы анкеты и обработку результатов этой формы:
public class HomeController : Controller
{
public ActionResult Index()
{
// TODO: those are obviously going to come from some data store
// or whatever comes to your mind
var model = new[]
{
new QuestionViewModel
{
Title = "Question 1",
PossibleAnswers = new[]
{
"Answer 1 to question 1",
"Answer 2 to question 1"
}
},
new QuestionViewModel
{
Title = "Question 2",
PossibleAnswers = new[]
{
"Answer 1 to question 2",
"Answer 2 to question 2",
"Answer 3 to question 2",
}
},
};
return View(model);
}
[HttpPost]
public ActionResult Index(IEnumerable<QuestionViewModel> questions)
{
// TODO : Process the answers. Here for each element of the
// questions collection you could use the Answer property
// in order to fetch the answer from the user.
return Content("Thqnks for submitting the questionnaire", "text/plain");
}
}
, а затем перейдем к представлению (~/Views/Home/Index.cshtml
):
@model IEnumerable<QuestionViewModel>
@using (Html.BeginForm())
{
<div>
@Html.EditorForModel()
</div>
<input type="submit" value="submit answers" />
}
и, наконец, шаблон редактора для вопроса, который будет отображаться для каждого элемента нашей коллекции моделей (~/Views/Home/EditorTemplates/QuestionViewModel.cshtml
):
@model QuestionViewModel
<h2>@Model.Title</h2>
@Html.HiddenFor(x => x.Title)
@foreach (var item in Model.PossibleAnswers)
{
@Html.RadioButtonFor(x => x.Answer, item)
@item
}