04节-使用EF CodeFirst来插入数据表
新增一个account控制器,添加一个regist的action,同时创建regist的视图。
访问 http://localhost:55751/Account/Regist 就会跳到我们刚才的页面。
视图中添加下面的表单:
@model WebApplicationCF01.Models.Account
@{
ViewBag.Title = "Regist";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>博客注册</h2>
<form action="Regist" method="post">
@Html.EditorForModel()
<input type="submit" value="提交" />
</form>
然后在控制器里面添加一个接收的action:
public ActionResult Regist()
{
return View();
}
[HttpPost]
public ActionResult Regist(Account model)
{
return View();
}
使用CodeFirst实现插入操作:
//添加到数据表里面
context.account.Add(model);
context.SaveChanges();
Previous: 03节-数据库初始化的问题
Next: 05节-使用codefirst实现数据库的查找