Scott Hanselmanの本(第1章)では、Createアクションメソッドの[HttpPost]を実装する2つのオプションがあります。[HttpPost] public ActionResult Create(FormCollectionコレクション)VERSUS [HttpPost] public ActionResult Create(夕食の夕食)
最初のフォームは、受信フォームフィールドに基づいてモデルオブジェクトを更新するためのTryUpdateModelに依存しています。受信フォームフィールドに無効な入力が含まれる場合、ModelState.IsValidはfalseに設定されます。
[HttpPost]
public ActionResult Create(Dinner dinner)
{
if (ModelState.IsValid)
{
dinnerRepository.Add(dinner);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinner.DinnerId });
}
else
return View(dinner);
}
は、どちらがより多くの生産に使用することをお勧めします。
[HttpPost]
public ActionResult Create(FormCollection collection)
{
Dinner dinner = new Dinner();
if (TryUpdateModel(dinner))
{
dinnerRepository.Add(dinner);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinner.DinnerId });
}
else
return View(dinner);
}
第二の方法は、次のようにアクションメソッドの引数を作成して渡されたモデルを利用するのですか?
お返事ありがとうございました。 – xport