在本节中,您会为电影控制器检查生成的操作方法和视图。然后,您将添加一个自定义的搜索页面。

运行该应用程序,然后浏览到Movies控制器通过将/Movies追加到您的浏览器的地址栏中的 URL。将鼠标指针悬停在编辑链接,看到它链接到的 URL。

ASP.NET MVC4 新手入门教程之六 ---6.编辑视图与编辑方法

编辑链接是由Html.ActionLink方法在Views\Movies\Index.cshtml视图中生成的:

@Html.ActionLink("Edit", "Edit", new { id=item.ID }) 

ASP.NET MVC4 新手入门教程之六 ---6.编辑视图与编辑方法

Html对象是一个帮助器,暴露使用上System.Web.Mvc.WebViewPage基类的属性。帮助器的 ActionLink 方法,便于动态生成 HTML 超链接链接到控制器上的操作方法。ActionLink方法的第一个参数是要呈现的链接文本 (例如,<a>Edit Me</a>)。第二个参数是要调用的操作方法的名称。最后一个参数是一个匿名对象,生成路由数据 (在本例中,ID 为 4 的)。

上图中所示的生成的链接是http://localhost:xxxxx/电影/编辑/4默认的路由 (建立在App_Start\RouteConfig.cs) 采用 URL 模式{controller}/{action}/{id}因此,ASP.NET 会http://localhost:xxxxx/电影/编辑/4转化为对Movies 控制器参数ID等于 4 的Edit操作方法的请求。检查App_Start\RouteConfig.cs文件中的以下代码。

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", 
            id = UrlParameter.Optional }
    );
}

你也可以通过使用查询字符串的操作方法参数。例如,URL http://localhost:xxxxx/电影/编辑? ID = 4也向Movies控制器的Edit操作方法传递参数ID为 4。

ASP.NET MVC4 新手入门教程之六 ---6.编辑视图与编辑方法

打开Movies控制器。两个Edit操作方法如下所示:

//
// GET: /Movies/Edit/5

public ActionResult Edit(int id = 0)
{
    Movie movie = db.Movies.Find(id);
    if (movie == null)
    {
        return HttpNotFound();
    }
    return View(movie);
}

//
// POST: /Movies/Edit/5

[HttpPost]
public ActionResult Edit(Movie movie)
{
    if (ModelState.IsValid)
    {
        db.Entry(movie).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(movie);
}

通知的第二个Edit操作方法的前面 HttpPost属性。此属性指定,过载的Edit方法可以调用只为 POST 请求。你可以将 HttpGet属性应用于第一种编辑方法,但这是不必要的因为它是默认值。(我们将引用他们作为HttpGet方法隐式地分配HttpGet属性的操作方法)。

HttpGetEdit方法获取电影 ID 参数、 查找电影使用实体框架Find方法,并返回到编辑视图的选定的影片。ID 参数指定默认值为零,如果不带参数调用该Edit 的方法。如果找不到一部电影,则会返回HttpNotFound 。当脚手架系统创建编辑视图时,它审查Movie课并创建代码来呈现<label><input>元素的每个类的属性。下面的示例演示了生成的编辑视图:

@model MvcMovie.Models.Movie

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Movie</legend>

        @Html.HiddenFor(model => model.ID)

        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ReleaseDate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ReleaseDate)
            @Html.ValidationMessageFor(model => model.ReleaseDate)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Genre)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Genre)
            @Html.ValidationMessageFor(model => model.Genre)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Price)
            @Html.ValidationMessageFor(model => model.Price)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

通知视图模板如何将@model MvcMovie.Models.Movie语句在文件的顶部 — — 这将指定视图预计的模型视图模板类型Movie.

搭建的代码使用的帮助器方法的几种简化的 HTML 标记。 Html.LabelFor帮助器将显示字段 ("标题"、"ReleaseDate"、"流派"或"价格") 的名称。 Html.EditorFor帮手会呈现 HTML <input>元素。Html.ValidationMessageFor帮助器将显示与该属性相关联的任何验证消息。

运行该应用程序,然后定位到/Movies URL。单击编辑链接。在浏览器中查看网页的源代码。如下所示的 HTML 表单元素。

<form action="/Movies/Edit/4" method="post">    <fieldset>
        <legend>Movie</legend>

        <input data-val="true" data-val-number="The field ID must be a number." data-val-required="The ID field is required." id="ID" name="ID" type="hidden" value="4" />

        <div class="editor-label">
            <label for="Title">Title</label>
        </div>
        <div class="editor-field">
            <input class="text-box single-line" id="Title" name="Title" type="text" value="Rio Bravo" />
            <span class="field-validation-valid" data-valmsg-for="Title" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label">
            <label for="ReleaseDate">ReleaseDate</label>
        </div>
        <div class="editor-field">
            <input class="text-box single-line" data-val="true" data-val-date="The field ReleaseDate must be a date." data-val-required="The ReleaseDate field is required." id="ReleaseDate" name="ReleaseDate" type="text" value="4/15/1959 12:00:00 AM" />
            <span class="field-validation-valid" data-valmsg-for="ReleaseDate" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label">
            <label for="Genre">Genre</label>
        </div>
        <div class="editor-field">
            <input class="text-box single-line" id="Genre" name="Genre" type="text" value="Western" />
            <span class="field-validation-valid" data-valmsg-for="Genre" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label">
            <label for="Price">Price</label>
        </div>
        <div class="editor-field">
            <input class="text-box single-line" data-val

相关文章:

  • 2021-04-17
  • 2021-04-20
  • 2021-07-06
  • 2021-12-04
  • 2021-12-29
  • 2021-11-04
  • 2021-12-23
猜你喜欢
  • 2021-07-13
  • 2022-01-15
  • 2021-06-07
  • 2022-03-01
  • 2021-11-15
相关资源
相似解决方案