2、GuestBook with model
GuestBook with modelModel:GuestBookEntry.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace Mvc1.Models{ public class GuestBookEntry { public string Name { get; set; } public string Email { get; set; } public string Comments { get; set; } }}
View: Index.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Mvc2.Models.GuestBookEntry>" %><asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Index</asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2> Sign the Guest Book!</h2> <%using (Html.BeginForm()) { %> <fieldset> <legend>Fields</legend> <p> <%=Html.LabelFor(model => model.Name)%> <%=Html.TextAreaFor(model =>model.Name) %> </p> <p> <%=Html.LabelFor(model => model.Email)%> <%=Html.TextAreaFor(model => model.Email)%> </p> <p> <%=Html.LabelFor(model => model.Comments)%> <%=Html.TextAreaFor(model => model.Comments)%> </p> <p> <input type="submit" value="Create" /> </p> </fieldset> <%} %></asp:Content>
Inherits="System.Web.Mvc.ViewPage<Mvc2.Models.GuestBookEntry>" %>
非常重要
View : ThankYou.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Mvc2.Models.GuestBookEntry>" %><asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">ThankYou</asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>ThankYou</h2> Thank you for signing our Guest Book.You entered:<br /> <%=Html.DisplayForModel() %></asp:Content>
Controller:GuestBookController.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using Mvc2.Models;namespace Mvc2.Controllers{ public class GuestBookController : Controller { // // GET: /GuestBook/ public ActionResult Index() { var model = new GuestBookEntry(); return View(model); } public ActionResult Index(GuestBookEntry entry) { TempData["entry"] = entry; return RedirectToAction("ThankYou"); } public ActionResult ThankYou() { if (TempData["entry"] == null) { return RedirectToAction("index"); } var model = (GuestBookEntry)TempData["entry"]; return View(model); } }}
2011-5-16 11:50 danny
页:
[1]