Asp.net mvc 2 in action 笔记1 概述、Model

受Rails成功的影响,很多的开发框架都加入了rails成功的MVC思想,如.net世界的Castle Subsonic等,微软发布的框架Asp.net MVC作为后来者,吸取了很多好的思想,而且作为微软的官方支持,代码开源,因此估计asp.net的Web开发世界这个会成为主流。

本系列是看的图书《Asp.net mvc 2 in action》的一个笔记整理

约定

■ Content—类似CSS 和图片的静态文件

■ Controllers—控制器类

■ Models—模型类

■ Scripts—JavaScript 文件

■ Views—视图

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:简单的形式

如控制器HomeController.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.net Entity,估计这个使用起来更方便,可以可视化的进行建模

原文地址:https://www.cnblogs.com/2018/p/2045900.html