2012-01-23 7 views
4

MVCは、データがブラウザにどのように送信されるかについて非常に明確です。あなたはURLに行き、コードを実行してモデルを作成し、その型付けされたモデルをビューに渡します。その後、モデルの状態に基づいてHTMLがレンダリングされます。データがポストバックされたときに、MVCがモデルをポピュレートする方法

しかし、ユーザーがページにフォームを送信すると、コントローラで使用するためにMVCがそのフォームをモデルにどのようにマップするのですか?

私は魔法を推測しているが、どこかで起こる:

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

しかし、私はGetting started with ASP.NET MVC 3を以下のよ、なぜ....

を理解していませんよ。簡単な参照のためのコードは以下の通りです。

コントローラー:

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> 

答えて

3

MVCはちょうど同じ名前のFormsCollectionオブジェクトの値に対するモデルのパブリックプロパティと一致します。名前と型が一致する場合、モデルのインスタンスが作成され、値がそれらのプロパティにコピーされます。

このプロセスはモデルバインディングと呼ばれ、カスタムモデルバインダーを作成できます。

EditorForは、モデルバインダーが理解できるようにフォーム入力に名前を付けるようにテンプレートを使用していますが、このプロセスはEditorForとはほとんど関係ありません。

+0

私は、あなただけのブレークポイントを入れて、mvcアクションをトレースすることができます –

2

MVCをいただきましRを使用していますポストバック値を取得してモデルを再作成するためのモデルバインダーとして認識されます。ここで

は良い読み物です:Models and Validation in ASP.NET MVC

3

ASP.Net MVCは、Configuration概念上の条約に基づいています。だから、ほとんどのものは魔法のように機能しますが、メカニズムの下では、デフォルトで働かせるだけでなく、必要に応じてカスタマイズすることもあります。 これを理解するには、Model Bindingという用語をチェックアウトする必要があります。

Check here

6-tips-for-asp-net-mvc-model-binding

関連する問題