asp.net-mvc - 当数据回发时,MVC 如何填充模型

标签 asp.net-mvc asp.net-mvc-3 razor

MVC对于如何将数据发送到浏览器非常清楚。您转到一个 URL,它运行代码来创建模型,将该类型化模型传递到 View 中,然后 View 根据模型的状态呈现 HTML。

但是,我发现不太清楚的是,当用户在页面上提交表单时,MVC 如何将该表单映射回模型以在 Controller 中使用?

我猜魔法发生在以下地方:

@Html.EditorFor(model => model.Title)

但我不明白为什么......

我正在关注Getting started with ASP.NET MVC 3 。下面的代码来自其中,以方便引用。

Controller :

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

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

查看:

@model MvcMovie.Models.Movie

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@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>

生成:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Edit</title>
    <link href="/Content/Site.css" rel="stylesheet" type="text/css" />
    <script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
    <script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
</head>
<body>
    <div class="page">
        <header>
            <div id="title">
                <h1>MVC Movie App</h1>
            </div>
           ...
        </header>
        <section id="main">


<h2>Edit</h2>

<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>

<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-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="true" data-val-number="The field Price must be a number." 
                data-val-required="The Price field is required." id="Price" name="Price" type="text" value="9.99" />
            <span class="field-validation-valid" data-valmsg-for="Price" data-valmsg-replace="true"></span>
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
</form>
<div>
    <a href="/Movies">Back to List</a>
</div>

        </section>
        <footer>
        </footer>
    </div>
</body>
</html>

最佳答案

MVC 只是将模型的公共(public)属性与同名的 FormsCollection 对象中的值进行匹配。如果名称和类型匹配,则会创建模型的实例并将值复制到这些属性中。

此过程称为模型绑定(bind),您可以创建自定义模型绑定(bind)器。

该过程与 EditorFor 直接关系不大,尽管 EditorFor 使用模板确保以模型绑定(bind)程序可以理解的方式命名表单输入。

关于asp.net-mvc - 当数据回发时,MVC 如何填充模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8976307/

相关文章:

c# - OWIN 能否替代 ASP.NET MVC 应用程序中的 DI?

asp.net-mvc - MVC,不再使用 HttpContext.Current "supposed"了吗?

c# - 在 DropDownFor 值更改时更改 EditorFor 的值

javascript - Knockout JS 验证不起作用

asp.net-mvc - 在 Razor 中使用 Html.EditorForModel 渲染时,ASP.Net [HiddenInput] 数据属性不起作用?

asp.net-mvc - MVC3 RenderPartial 跨多个页面缓存

c# - 使用 Aspx 页面作为 razor View 的局部 View

c# - 使用 LINQ 或 C# 中的其他方式对输入文件中的行进行分组

asp.net-mvc - 使用 HTML Action 链接指向一个 div

c# - 如何将数据传递给 _Layout.cshtml