私はモデルを持っています
public class QuestionViewModel
{
public string Text { get; set; }
public int Id { get; set; }
public bool IsMultiSelected { get; set; }
public IEnumerable<RadioButtonViewModel> AnswerViewModels { get; set; }
}
public class RadioButtonViewModel
{
public string Value { get; set; }
public string Name { get; set; }
}
そしてテストコントローラー
[HttpGet]
public ActionResult ProcessAnswers()
{
var question1 = new List<RadioButtonViewModel>
{
new RadioButtonViewModel
{
Value = "1",
Name = "Radio 1 option 1"
},
new RadioButtonViewModel
{
Value = "2",
Name = "Radio 1 option 2"
},
new RadioButtonViewModel
{
Value = "3",
Name = "Radio 1 option 3"
}
};
var question2 = new List<RadioButtonViewModel>
{
new RadioButtonViewModel
{
Value = "1",
Name = "Radio 2 option 1"
},
new RadioButtonViewModel
{
Value = "2",
Name = "Radio 2 option 2"
},
new RadioButtonViewModel
{
Value = "3",
Name = "Radio 2 option 3"
}
};
var qs = new List<QuestionViewModel>
{
new QuestionViewModel
{
Id = 1,
Text = "Please select correct variant in question 1",
AnswerViewModels = question1
},
new QuestionViewModel
{
Id = 2,
Text = "Please select correct variant in question 2",
AnswerViewModels = question2
}
};
return View("TestRadioView", qs);
}
[HttpPost]
public ActionResult ProcessAnswers(List<QuestionViewModel> answers)
{
return RedirectToAction("Index");
}
私の見解
@using System.Web.Mvc.Html
@using ExpertApplication.ViewModels
@model List<QuestionViewModel>
@{
ViewBag.Title = "Test Radio Button View";
}
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
</head>
<body>
<div>
@if(Model != null)
{
<br />
using(@Html.BeginForm("ProcessAnswers", "Home", FormMethod.Post))
{
foreach(var question in Model)
{
<h2>@question.Text</h2>
<br />
foreach(var radioButtonViewModel in question.AnswerViewModels)
{
@Html.RadioButton(question.Id.ToString(), radioButtonViewModel.Value)
@Html.Label(question.Id.ToString(), radioButtonViewModel.Name)
<br />
}
}
<input type="submit" value="Send" />
}
}
</div>
</body>
</html>
アップデート
見方やモデルを変えても正解がない
public IList<RadioButtonViewModel> AnswerViewModels { get; set; }
@using System.Web.Mvc.Html
@using ExpertApplication.ViewModels
@model List<QuestionViewModel>
@{
ViewBag.Title = "Test Radio Button View";
}
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
</head>
<body>
<div>
@if(Model != null)
{
if(ViewData["Message"] != null)
{
<h1>@ViewData["Message"]</h1>
}
else
{
<h1>Please answer the questions</h1>
}
<br />
using(@Html.BeginForm("ProcessAnswers", "Home", FormMethod.Post))
{
for(int i = 0; i < Model.Count(); i++)
{
var question = Model[i];
<h2>@question.Text</h2>
<br />
for(int j = 0; j < question.AnswerViewModels.Count(); j++)
{
var radioButtonViewModel = question.AnswerViewModels[j];
@Html.RadioButton(string.Format("QuestionViewModel[{0}].AnswerViewModels.Id", question.Id), radioButtonViewModel.Value)
@Html.Label(string.Format("{0}", question.Id), radioButtonViewModel.Name)
<br />
}
}
<input type="submit" value="Send" />
}
}
</div>
</body>
</html>
実際の ProcessAnswers 変数では、リストの回答は null です。私のトラブルはどこですか?