2、GuestBook with model

GuestBook with model

Model:GuestBookEntry.cs

[code]
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; }
}
}
[/code]

View: Index.aspx
[code]
<%@ 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>
[/code]

Inherits="System.Web.Mvc.ViewPage<Mvc2.Models.GuestBookEntry>" %>
非常重要

View : ThankYou.aspx
[code]
<%@ 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>
[/code]

Controller:GuestBookController.cs
[code]
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);
}
}
}
[/code]


2011-5-16 11:50 danny
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值