读保哥《ASP.NET MVC2开发实战》第一回(第一个项目)

 

   首次做MVC小项目,感觉挺好的,下面来展示下我的第一个MVC小项目:

1:文件布局

2.数据表

3.

CS_Guestbook类代码:

 

GuestbookController代码:

using System; using System.Collections.Generic;

using System.Linq;

using System.Web;

 using System.Web.Mvc;

using Guestbook.Models;

using Guestbook.Controllers;

namespace Guestbook.Controllers {    

public class GuestbookController : Controller    

 {         //         // GET: /Guestbook/

        public ActionResult Index()        

 {

            DB_GuestbookEntities db = new DB_GuestbookEntities();       

          var data = db.TB_Guestbook;

                        return View(data);         }        

public ActionResult Create()        

{            

return View();        

 }        

 [HttpPost]        

 public ActionResult Save(CS_Guestbook csdb)        

{            

DB_GuestbookEntities db = new DB_GuestbookEntities();            

db.AddToTB_Guestbook(new TB_Guestbook()            

{                 Name = csdb.Name,  

               Email = csdb.Email,

                Message = csdb.Message,

                DataTime = DateTime.Now

            });

            db.SaveChanges();

            ViewData["Name"] = csdb.Name;  

           ViewData["Email"] = csdb.Email;  

           ViewData["Message"] = csdb.Message;

            ViewData["DateTime"] = DateTime.Now;

            return View();        

}

             public ActionResult Edit(CS_Guestbook csdb)        

 {                   

 DB_GuestbookEntities db = new DB_GuestbookEntities();

             var data = db.TB_Guestbook.First(x => x.Id==csdb.Id);

            data.Name = csdb.Name;

             data.Message = csdb.Message;

            data.Email = csdb.Email;

            db.SaveChanges();

            return View();

         }

        public ActionResult Delete(int Id)

        {

            DB_GuestbookEntities db = new DB_GuestbookEntities();

            var data = db.TB_Guestbook.First(x => x.Id == Id);

            db.DeleteObject(data);

             db.SaveChanges();

            Response.Redirect("/Guestbook/Index");

            return View();

        }

          }

}

4.IndexView代码:

 

 

   <h2>留言显示</h2>

 

    <table>        

 <tr>            

<th></th>            

<th>                 Id             </th>            

<th>                 Name             </th>            

<th>                 Email             </th>            

<th>                 Message             </th>            

<th>                 DataTime             </th>        

</tr>

 

    <% foreach (var item in Model) { %>            

 <tr>            

<td>                

<%: Html.ActionLink("更新", "Edit", new { id = item.Id })%>                              

<%: Html.ActionLink("删除", "Delete", new { id=item.Id })%>            

</td>            

<td>                

<%: item.Id %>

</td>            

<td>                

<%: item.Name %>            

</td>            

<td>                

 

<%: item.Email %>            

</td>            

<td>                

<%: item.Message %>            

</td>            

<td>                

<%: String.Format("{0:g}", item.DataTime) %>            

</td>        

</tr>        

<% } %>

 

    </table>

 

    <p>         <%: Html.ActionLink("留下足迹", "Create") %>     </p>

5.CreateView代码:

 

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Guestbook.Controllers.CS_Guestbook>" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">  Create </asp:Content>

 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>请留言</h2>    <% Html.EnableClientValidation(); %>    <%=Html.ValidationSummary() %>    <% using (Html.BeginForm("Save", RouteData.Values["controller"].ToString()))       { %>         <%:Html.LabelFor(x => x.Name)%>    <%:Html.TextBoxFor(x => x.Name)%>    <%:Html.ValidationMessageFor(x=>x.Name) %>    <br/><br/>   <%:Html.LabelFor(x => x.Email)%>   <%:Html.TextBoxFor(x => x.Email)%>   <%:Html.ValidationMessageFor(x=>x.Email) %>    <br/><br/>   <%:Html.LabelFor(x => x.Message)%>   <%:Html.TextAreaFor(x => x.Message)%>   <%:Html.ValidationMessageFor(x=>x.Message) %>    <br/>    <input type="submit" value="猛点我便可留言!!" />       <%} %>  

 

</asp:Content>

6.EditView代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Guestbook.Controllers.CS_Guestbook>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">  Edit </asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Edit</h2>

    <% using (Html.BeginForm()) {%>         <%: Html.ValidationSummary(true) %>                 <fieldset>             <legend>Fields</legend>                         <div class="editor-label">                 <%: Html.LabelFor(model => model.Id) %>             </div>             <div class="editor-field">                 <%: Html.TextBoxFor(model => model.Id) %>                 <%: Html.ValidationMessageFor(model => model.Id) %>             </div>                         <div class="editor-label">                 <%: Html.LabelFor(model => model.Name) %>             </div>             <div class="editor-field">                 <%: Html.TextBoxFor(model => model.Name) %>                 <%: Html.ValidationMessageFor(model => model.Name) %>             </div>                         <div class="editor-label">                 <%: Html.LabelFor(model => model.Email) %>             </div>             <div class="editor-field">                 <%: Html.TextBoxFor(model => model.Email) %>                 <%: Html.ValidationMessageFor(model => model.Email) %>             </div>                         <div class="editor-label">                 <%: Html.LabelFor(model => model.Message) %>             </div>             <div class="editor-field">                 <%: Html.TextBoxFor(model => model.Message) %>                 <%: Html.ValidationMessageFor(model => model.Message) %>             </div>                                                 <p>                 <input type="submit" value="猛点我来更新" />             </p>         </fieldset>

    <% } %>

    <div>         <%: Html.ActionLink("返回首页", "Index") %>     </div>

</asp:Content>

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/wangheblog/archive/2012/04/03/little_bird.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值