モデルまたはビューモデルから配列を受け取るようにコントローラーを作成することはできますが、配列はORM対応ではありません(ドメインモデルを使用してビューを直接表示する場合)。そのことを念頭に置いて、コントローラーのビューはIList
;で動作します。IList
柔軟性があり、配列とは異なり、要素の数による制約を受けません。IListは、アレイと比較してORM対応です。
The only adjustment we should make is in view; HTML or rather javascript, doesn't have notion of list, it can only use an array or associative array. Acknowledging this limitation, we will emit those items from List<ClassType>
to javascript-friendly arrays.
Finally, when we submit, our friendly neighborhood ASP.NET MVC will take care of these matters for us, i.e. it can automatically populate our List<ClassType>
variables based on the submitted arrays from HTML/javascript, i.e. there is no need for us to iterate the Request.Form
, no need for manual population, everything our code operates on is Model/ViewModel-centric as they are ought to be. ASP.NET MVC coders' life is wonderful isn't it? ;-)
Complete code below:
Model:
namespace SoQna.Models
{
public class Question
{
public int QuestionId { get; set; }
public string QuestionText { get; set; }
}
public class Answer
{
public int AnswerId { get; set; }
public Question Question { get; set; }
public string AnswerText { get; set; }
}
}
ViewModel:
using System.Collections.Generic;
namespace SoQna.ViewModels
{
public class QnaViewModel
{
public IList<AnswerToQuestion> Answers { get; set; }
}
public class AnswerToQuestion
{
public int ToQuestionId { get; set; }
public string QuestionText { get; set; }
public string AnswerText { get; set; }
}
}
Controller:
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace SoQna.Controllers
{
public class HomeController : Controller
{
IList<SoQna.Models.Question> _qnaRepo = new List<SoQna.Models.Question>
{
new SoQna.Models.Question { QuestionId = 1,
QuestionText =
"Why is there a proliferation of different kind of ORMs?"
},
new SoQna.Models.Question {
QuestionId = 2, QuestionText = "How are you?"
},
new SoQna.Models.Question {
QuestionId = 3, QuestionText = "Why the sky is blue?"
},
new SoQna.Models.Question {
QuestionId = 4, QuestionText = "Why is MVC the bees knees?"
},
};
public ActionResult Index()
{
var qna = new SoQna.ViewModels.QnaViewModel {
Answers = new List<SoQna.ViewModels.AnswerToQuestion>()
};
foreach (var question in _qnaRepo)
{
if (question.QuestionId == 1) continue; // subjective :-)
qna.Answers.Add(
new SoQna.ViewModels.AnswerToQuestion {
ToQuestionId = question.QuestionId,
QuestionText = question.QuestionText,
AnswerText = "Put your answer here"
}
);
}
return View(qna);
}
[HttpPost]
public ViewResult SubmitAnswers(SoQna.ViewModels.QnaViewModel a)
{
foreach (var answer in a.Answers)
{
answer.QuestionText = _qnaRepo.Single(x =>
x.QuestionId == answer.ToQuestionId).QuestionText;
}
return View(a);
}
}//HomeController
}//namespace
View for Home/Index
@model SoQna.ViewModels.QnaViewModel
@{
ViewBag.Title = "Index";
}
<h2>Hahah</h2>
@using (Html.BeginForm("SubmitAnswers", "Home"))
{
int i = 0;
foreach (var answer in Model.Answers)
{
@: Question #@(answer.ToQuestionId) <br />
@Html.Hidden("Answers[" + i + "].ToQuestionId", answer.ToQuestionId)
@Html.Label("Answers[" + i + "].QuestionText", answer.QuestionText)
@*
to save bandwidth we didn't include QuestionText on submit,
this is done by assigning it on Label, and no need to persist
QuestionText on Hidden
*@
<br />
@Html.TextArea("Answers[" + i + "].AnswerText", answer.AnswerText)
<hr />
++i;
}
<input type="submit" value="Done" />
}
View for Home/SubmitAnswers
@model SoQna.ViewModels.QnaViewModel
@{
ViewBag.Title = "SubmitAnswers";
}
<h2>SubmitAnswers</h2>
@foreach (var item in Model.Answers)
{
@: Answer to question: @item.QuestionText
<br />
@item.AnswerText
<hr />
}