C#
対応4: MessageBox ↔ TempData + RedirectToAction(PRGパターン)
出典: Controller は WinForms の Form_Load 拡張版だと理解する — ASP.NET MVC 5 業務SE 入門 — 対応4: MessageBox ↔ TempData + RedirectToAction(PRGパターン)
// ✅ ASP.NET MVC版: TempData + RedirectToAction(PRGパターン)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(CustomerVm model)
{
_service.Save(model);
TempData["Message"] = "保存しました";
return RedirectToAction("Index"); // Post-Redirect-GetでF5二重登録を予防
}
public ActionResult Index()
{
// TempDataは次のリクエストまで生存する→リダイレクト先で読める
if (TempData["Message"] != null)
{
ViewBag.Message = TempData["Message"];
}
return View(_service.GetAll());
}
▸ この snippet は実行結果未収録
▸ 実行結果は未収録です
Source収録記事
この snippet は記事の「対応4: MessageBox ↔ TempData + RedirectToAction(PRGパターン)」セクションに登場する。コードの前後の文脈・ハマりどころの解説は記事本文で。
同じ記事から
8 件// ✅ WinForms版: Form_Loadで初期化処理 public partial class CustomerForm : Form { private void CustomerForm_Load(object sender, EventArgs e)未収録
対応1: Form_Load ↔ Index()Action
#d42b77bc5cba
// ✅ ASP.NET MVC版: Index()Actionで同等の処理 public class CustomerController : Controller { public ActionResult Index()未収録
対応1: Form_Load ↔ Index()Action
#dc78e1f93429
// ✅ WinForms版:ボタンクリックでフォーム送信 public partial class SearchForm : Form { private void btnSearch_Click(object sender, EventArgs e)未収録
対応2: btn_Click ↔ POST Action
#8e21b9ee15b3
// ✅ ASP.NET MVC版: POST Actionで同等 public class CustomerController : Controller { private readonly ICustomerService _service;未収録
対応2: btn_Click ↔ POST Action
#de3edf727181
// ✅ WinForms版: DataSource直接代入 private void Form_Load(object sender, EventArgs e) { var list = new List<CustomerVm>未収録
対応3: DataGridView.DataSource ↔ return View(model)
#524489acb479
// ✅ ASP.NET MVC版: return View(model)でModelを渡す public ActionResult Index() { var list = new List<CustomerVm>未収録
対応3: DataGridView.DataSource ↔ return View(model)
#401e976a96e9
