zoukankan      html  css  js  c++  java
  • 读保哥《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>

  • 相关阅读:
    c# WinForm 定时执行某个后台操作 如把B文件夹下的文件Copy到A文件夹下
    c# 创建指定大小的空字符填充的文本文件 在指定位置读写相关内容
    c# DirectShow 通过IAMVideoProcAmp的Set方法 来设置视频图像的Brightness 调整亮度
    [转]灰度图像的腐蚀算法和细化算法(C#代码)
    利用fleximage实现图片上传
    利用acts_as_ferret实现全文检索
    纯CSS无hacks的跨游览器多列布局
    IE私有CSS样式属性一览
    利用thinking sphinx实现全文检索
    搭建rails运行环境
  • 原文地址:https://www.cnblogs.com/wangheblog/p/little_bird.html
Copyright © 2011-2022 走看看