Guestbook

Guestbook

留言板(guestbook)比较简单:何人何时留何言。

1.运行 vs2012,选择 MVC 4 模板,输入项目名称:x01.Guestbook,一路默认即可。

2.在项目上右击,从弹出菜单上选择 NuGet 管理器,分别安装 EntityFramwork 和   EntityFramwork.SqlServerCompact,以便使用 DbContext。

3.在 App_Data 文件夹下,创建 Compact 数据库 Guestbook.sdf。表 Entry,字段 Id, Name, Message, Date

4.在 Models 文件夹下,添加 GuestbookModel.cs 文件,内容如下:

GuestbookModel
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

namespace x01.Guestbook.Models
{
    public class GuestbookContext : DbContext
    {
        public GuestbookContext()
            : base("Guestbook")
        {
        }

        public DbSet<Entry> Entries { get; set; }
    }

    public class Entry
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Message { get; set; }
        public DateTime Date { get; set; }
    }

    public interface IGuestbookReponsitory
    {
        IList<Entry> GetEntries();
        Entry Find(int id);
        void Add(Entry entry);
    }

    public class GuestbookReponsitory : IGuestbookReponsitory
    {
        GuestbookContext _context = new GuestbookContext();

        public IList<Entry> GetEntries()
        {
            var result = from e in _context.Entries
                         orderby e.Date descending
                         select e;
            return result.Take(20).ToList();
        }

        public Entry Find(int id)
        {
            var r = _context.Entries.Find(id);
            return r;
        }

        public void Add(Entry entry)
        {
            entry.Date = DateTime.Now;
            _context.Entries.Add(entry);
            _context.SaveChanges();
        }
    }
}
复制代码
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

namespace x01.Guestbook.Models
{
    public class GuestbookContext : DbContext
    {
        public GuestbookContext()
            : base("Guestbook")
        {
        }

        public DbSet<Entry> Entries { get; set; }
    }

    public class Entry
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Message { get; set; }
        public DateTime Date { get; set; }
    }

    public interface IGuestbookReponsitory
    {
        IList<Entry> GetEntries();
        Entry Find(int id);
        void Add(Entry entry);
    }

    public class GuestbookReponsitory : IGuestbookReponsitory
    {
        GuestbookContext _context = new GuestbookContext();

        public IList<Entry> GetEntries()
        {
            var result = from e in _context.Entries
                         orderby e.Date descending
                         select e;
            return result.Take(20).ToList();
        }

        public Entry Find(int id)
        {
            var r = _context.Entries.Find(id);
            return r;
        }

        public void Add(Entry entry)
        {
            entry.Date = DateTime.Now;
            _context.Entries.Add(entry);
            _context.SaveChanges();
        }
    }
}
复制代码

5.在 Controllers 文件夹下,添加 GuestbookController.cs 文件,内容如下:

GuestbookController
using System.Web.Mvc;
using x01.Guestbook.Models;

namespace x01.Guestbook.Controllers
{
    public class GuestbookController : Controller
    {
        IGuestbookReponsitory _reponsitory = new GuestbookReponsitory();

        public ActionResult Index()
        {
            var m = _reponsitory.GetEntries();
            return View(m);
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(Entry entry)
        {
            if (ModelState.IsValid)
            {
                _reponsitory.Add(entry);
                return RedirectToAction("Index");
            }
            return View(entry);
        }

    }
}
复制代码
using System.Web.Mvc;
using x01.Guestbook.Models;

namespace x01.Guestbook.Controllers
{
    public class GuestbookController : Controller
    {
        IGuestbookReponsitory _reponsitory = new GuestbookReponsitory();

        public ActionResult Index()
        {
            var m = _reponsitory.GetEntries();
            return View(m);
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(Entry entry)
        {
            if (ModelState.IsValid)
            {
                _reponsitory.Add(entry);
                return RedirectToAction("Index");
            }
            return View(entry);
        }

    }
}
复制代码

6.在 Views/Guestbook 下,添加 Indes.cshtml, Add.cshtml 文件,内容如下:

Index
@model IEnumerable<x01.Guestbook.Models.Entry>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Add Message", "Add")
</p>

<section style="background-color:#fcfcfc; font-size:large;">
    @foreach (var item in Model)
    {
        <p><b>@item.Name (@item.Date.ToString()): </b><br />@item.Message</p>
    }
</section>
复制代码
@model IEnumerable<x01.Guestbook.Models.Entry>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Add Message", "Add")
</p>

<section style="background-color:#fcfcfc; font-size:large;">
    @foreach (var item in Model)
    {
        <p><b>@item.Name (@item.Date.ToString()): </b><br />@item.Message</p>
    }
</section>
复制代码
Add
@model x01.Guestbook.Models.Entry

@{
    ViewBag.Title = "Add";
}

<h2>Add</h2>

<form method="post">
    Name:
    <br />
    @Html.TextBoxFor(m => m.Name)
    <br />
    Message:
    <br />
    @Html.TextAreaFor(m => m.Message)
    <br />
    <input type="submit" value="Submit" />
</form>
复制代码
@model x01.Guestbook.Models.Entry

@{
    ViewBag.Title = "Add";
}

<h2>Add</h2>

<form method="post">
    Name:
    <br />
    @Html.TextBoxFor(m => m.Name)
    <br />
    Message:
    <br />
    @Html.TextAreaFor(m => m.Message)
    <br />
    <input type="submit" value="Submit" />
</form>
复制代码

7.修改 Shared/_layout.cshtml 文件中的菜单如下:

<li>@Html.ActionLink("Guestbook","Index","Guestbook")</li>

 按 F5 运行无误,则宣告完成。

 
 
分类:  C#

你可能感兴趣的:(OO)