六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 54|回复: 0

2、GuestBook with model

[复制链接]

升级  57%

115

主题

115

主题

115

主题

举人

Rank: 3Rank: 3

积分
371
 楼主| 发表于 2013-1-26 14:00:31 | 显示全部楼层 |阅读模式
GuestBook with model

Model: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);        }        [HttpPost]        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
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表