Asp.Netmvc 2 in actionノート-1概要、Model

6074 ワード

Railsの成功の影響を受けて、多くの開発フレームワークはrailsの成功したMVC思想に参加した.NetワールドのCastle Subsonicなど、マイクロソフトが発表したフレームワークAsp.NetMVCは後者として、多くの良い思想を吸収して、その上マイクロソフトの公式の支持として、コードはオープンソースで、そのためaspを推定します.NetのWeb開発世界はこれが主流になります.
本シリーズは読んでいる本『Asp.net mvc 2 in action』の1つのノートの整理です
に約束
■Content:CSSや画像のような静的ファイル
■コントローラ-コントローラ類
■Models—モデルクラス
■Scripts—JavaScriptファイル
■View-ビュー
Model
第2,8章
presentation model弱型
ViewData TempDataを使用して渡されたデータモデルです.この構造は辞書です.
ViewData:ビューのコンテナにデータを渡す
TempData is a collection that you can use to store data. It will be persisted in server Session memory for one round-trip.
例1:簡単な形式
コントローラのようにcsへの転送
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";
            return View();
        }

index.aspxs使用
<%= Html.Encode(ViewData["Message"])

例2:データ送信
Index.aspx
<form method="post" action="/GuestBook/Sign">
        <%= Html.Label("Name") %>
        <%= Html.TextBox("Name") %>
        
        <%= Html.Label("Email") %>
        <%= Html.TextBox("Email") %>
        
        <%= Html.Label("Comments") %>
        <%= Html.TextArea("Comments", new { rows=6, cols=30 }) %>
        <input type="submit" value="Sign" />
    </form>
 
   GuestBookController.cs       ,      
 public ActionResult Sign(string name, string email, string comments)
        {
            //do something with the values, such as send an email
 
            ViewData["name"] = name;
            ViewData["email"] = email;
            return View("ThankYou");
        }

presentation model強タイプ
前例2の強化
まずモデルを定義する
    public class GuestBookEntry
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string Comments { get; set; }
    }
Index.aspx  ViePage<T>   [IDE Add View    ]
Inherits="System.Web.Mvc.ViewPage<GuestBookWithModel.Models.GuestBookEntry>" %>
   <% using (Html.BeginForm()) {%>
         <fieldset>
            <legend>Fields</legend>
            <p>
                <%= Html.LabelFor(model => model.Name) %>
                <%= Html.TextBoxFor(model => model.Name) %>                
            </p>
            <p>
                <%= Html.LabelFor(model => model.Email) %>
                <%= Html.TextBoxFor(model => model.Email) %>                
            </p>
            <p>
                <%= Html.LabelFor(model => model.Comments) %>
                <%= Html.TextAreaFor(model => model.Comments) %>                
            </p>
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
 
    <% } %>    

コントローラGuestBookController.cs
       public ActionResult Index()
        {
            var model = new GuestBookEntry();
            return View(model);
        }
 
        [HttpPost]
        public ActionResult Index(GuestBookEntry entry)
        {
            //hang on to the submitted value, so we can
            //retrieve it upon redirect
            TempData["entry"] = entry;
            return RedirectToAction("ThankYou");
        }
 
        public ActionResult ThankYou()
        {
            if(TempData["entry"] == null)
            {
                //somehow they got here without filling out the form
                return RedirectToAction("index");
            }
 
            var model = (GuestBookEntry) TempData["entry"];
            return View(model);
        }

以上より,強いモデルはメンテナンス性を向上させ,ビューの自動化処理をサポートした[モデルのメタデータ情報が存在するため]
domain models
23章では、Nhibernate構築レルムモデルの詳細な例を示します.
もちろん今はAdoがいますNetEntityは、これが使いやすく、可視化してモデリングできると推定しています