MVC5学习系列——详解编辑操作

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 =>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值