using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Mvc5App.Models
{
public class TestViewModel
{
[Display(Name = "番号")]
[Required(ErrorMessage = "{0} は必須")]
public int No { set; get; }
[Display(Name = "プロジェクト名")]
[Required(ErrorMessage = "{0} は必須")]
[StringLength(10, ErrorMessage = "{0} は {1} 文字以内")]
public string Project { set; get; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using Mvc5App.Models;
namespace Mvc5App.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
// ・・・中略・・・
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "No, Project")] TestViewModel model)
{
if (ModelState.IsValid)
{
// INSERT 処理
return RedirectToAction("Index");
}
return View(model);
}
}
}
(3) View
Visual Studio で、上の Controller のコードのアクションメソッドを右クリックして表示されるメニューに従って View を自動生成させます。Template と Model Class の設定に注意してください。