MVC5学习笔记,其实就是敲了一遍官网代码,官网地址:http://www.asp.net/mvc
接着上一篇 MVC5学习系列——从控制器访问模型的数据(添加、修改、删除),这次我们详细说一下编辑操作,不过在这之前先修改一下列表页面。先将页面中的英文处理掉,还有就是如果你创建了一个不是空的MVC程序,那么程序中默认集成了Bootstrap ,这里我们也自己动手添加Bootstrap 样式。按照自动生成的MVC非空程序模板,我们也把样式文件放到根目录的Content文件夹中。
看一下修改之后的Index.cshtml页面:
@model IEnumerable<DDZ.MVC5Test.Models.Movie>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>我的电影</title>
<link href="~/Content/Bootstrap/3.3.6/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<p>
@Html.ActionLink("添加电影", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.ReleaseDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.ActionLink("编辑", "Edit", new { id = item.ID }) |
@Html.ActionLink("详情", "Details", new { id = item.ID }) |
@Html.ActionLink("删除", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
</div>
</body>
</html>
还有修改之后的Edit.cshtml页面:
@model DDZ.MVC5Test.Models.Movie
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>我的电影—编辑</title>
<link href="~/Content/Bootstrap/3.3.6/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>我的电影—编辑</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ID)
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ReleaseDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ReleaseDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ReleaseDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model =>