私のモデルは次のとおりです。
public class Answer
{
public string AnswerText { get; set; }
public static List<Answer> GetAnswers(string[] answers)
{
List<Answer> _answers = new List<Answer>();
foreach (string _answer in answers)
{
Answer _answer1 = new Answer()
{
AnswerText = _answer
};
_answers.Add(_answer1);
}
return _answers;
}
}
public class Question
{
[Required]
public string QuestionText { get; set; }
}
public class QuestionAnswerModel
{
public List<Answer> Answers { get; set; }
[Required]
public Question Question { get; set; }
私のコントローラーは:
[HttpPost]
// ビューのテキストボックスを変更して投稿する場合、この _model は null です
public ActionResult A(QuestionAnswerModel _model)
{
QuestionAnswerModel _questionanswerModel = new QuestionAnswerModel();
// _questionanswerModel.Answers = Answer.GetAnswers(_response.GetAnswerResult);
_questionanswerModel.Question = new Question();
return View(_questionanswerModel);
}
[HttpGet]
public ActionResult A(string question)
{
QuestionAnswerModel _questionanswerModel = new QuestionAnswerModel();
// _questionanswerModel.Answers = Answer.GetAnswers(_response.GetAnswerResult);
_questionanswerModel.Question = new Question() {
QuestionText = question
};
return View(_questionanswerModel);
}
私の見解は次のとおりです。
@model ProjectnameSample.Models.QuestionAnswerModel
@{
ViewBag.Title = "A";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>A</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>QuestionAnswerModel</legend>
@Html.TextBoxFor(m => m.Question.QuestionText)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
アクションに HttpPost と HttpGet がありますが、テキスト ボックスを変更すると、テキスト ボックスのテキストがパラメーターを通過せず、Model が nullQuestion
でAnswers
、QuestionAnswerModel
ビュー用に2つのモデルがあります。答えと問題。
私が間違っていることはありますか?