簡易留言簿系統-ASP.NET MVC (後台基本完成)

來源:互聯網
上載者:User

標籤:action   全面   control   mvc   web   註冊   address   資訊   lap   

今天在工作的空餘,完成了簡易留言簿系統的準系統。在這裡總結全面總結一下。

簡易留言薄系統: 開發工具:vs2013

在資料庫方面,我選擇了SQL 2012。

在代碼開發階段,我沒有用code first的自動產生的資料庫,而是指定了SQL伺服器與資料庫的資訊。

通過這個系統學習到的東西。

1. 實現了在同一頁面中顯示資料與錄入資料。剛開始的時候,由於對於MVC學習的東西很死板,所以一直沒有想到應該如何?,但是後來通過在網上的諮詢,以及自己的理解,發現很簡單,那就是 使用 BeginForm("insert資料的Action名稱",“所在的Controller名”)。

代碼如下:

@model MvcBBSApplication.Models.ReplyMessageViewModel<h2>ReplyMessage</h2><fieldset>    <legend>MessageInformation</legend>        <div class="display-label">        <b>@Html.DisplayNameFor(model => model.Tile)</b>    </div>    <div class="display-field">        @Html.DisplayFor(model => model.Tile)    </div>    <div class="display-label">        <b>@Html.DisplayNameFor(model => model.MessageContent)</b>    </div>    <div class="display-field">        @Html.DisplayFor(model => model.MessageContent)    </div>    <div class="display-label">        <b>@Html.DisplayNameFor(model => model.MessageCreatOn)</b></div>    <div class="display-field">        @Html.DisplayFor(model => model.MessageCreatOn)    </div>    <div class="display-label">        <b>@Html.DisplayNameFor(model => model.NickName)</b></div>    <div class="display-field">        @Html.DisplayFor(model => model.NickName)    </div>    <table style="border:solid;">        <tr>            <td colspan="6">                <legend>ReplyInformation</legend>            </td>        </tr>        @foreach (var item in Model.ReplyList)        {            <tr>                <td>                    <b>@Html.DisplayNameFor(model => item.Content)</b>                </td>                <td>                    @Html.DisplayFor(model => item.Content)                </td>                <td>                    <b>@Html.DisplayNameFor(model => item.CreatOn)</b>                </td>                <td>                    @Html.DisplayFor(model => item.CreatOn)                </td>                <td>                    <b>@Html.DisplayNameFor(model => item.User.NickName)</b>                </td>                <td>                    @Html.DisplayFor(model => item.User.NickName)                </td>            </tr>        }    </table>------------以上的代碼實現的是顯示資料------------------------------以下的代碼實現的是寫入資料到SQL-----------    @using (Html.BeginForm("InsertReply", "Message"))    {              <legend>ReplyMessage</legend>     <table>         <tr>             <td>                 Content:@Html.TextBox("Content")                 @Html.Hidden("Id", Model.messageId)                              </td>             <td>                 <input type="submit" value="Reply" />               </td>         </tr>     </table>    }----------------------end------------------------------</fieldset><p>   @Html.ActionLink("Back to List", "ListMessage","Message")</p>
View層
MvcBbsConnect db = new MvcBbsConnect();//顯示頁面資料的Action        public ActionResult ReplyMessage(int id)        {            var message = db.Messages.Where(p => p.Id == id).FirstOrDefault();                        List<Reply> listReply = db.Replys.Where(p => p.Message.Id == id).ToList();            ReplyMessageViewModel rm = new ReplyMessageViewModel()            {                Tile = message.Title,                MessageContent = message.content,                MessageCreatOn = message.CreatOn,                NickName = message.User.NickName,                ReplyList = listReply            };            return View(rm);        }        //寫入資料到資料庫的action        [HttpPost]        public ActionResult InsertReply()        {            int messageid = int.Parse(Request.Form["Id"].ToString());            var message = db.Messages.Where(p => p.Id == messageid).FirstOrDefault();            var user = db.Users.Where(p => p.Email == User.Identity.Name).FirstOrDefault();                        Reply re = new Reply(){                CreatOn = DateTime.Now,                User = user,                Message = message,                Content = Request.Form["Content"],                Ip = Request.UserHostAddress.ToString()            };            db.Replys.Add(re);            db.SaveChanges();            return RedirectToAction("ReplyMessage", "Message", new { Id = messageid});        }
controller層

2.學到了如何建立一個簡單的ASP.NET MVC工程,以及實現簡單的CRUD的功能。

3.開始接觸了簡單的AJAX。

另外,在學習的過程中,還是練習了一下在ASP.NET MVC中如何使用webform時期連結資料庫的方法。同時,通過這個網站的開發,我對MVC的關注點分離有了一些體會,同時深刻體會到MVC的前台頁面確實比Webform 的頁面更簡潔,在開發過程中層與層之間的劃分,職責更加明確。同時MVC沒有了伺服器端控制項,使得開發的靈活度更大了。

 

簡易留言簿系統的功能基本實現:會員註冊,登入。留言的發布,刪除,回複留言的發布,刪除。但是網站的安全,註冊發送確認郵件,前台設計等都還沒有實現。

接下來的需要做的是前台頁面的設計,同時進行下一個網站(互動媒體學習社區)的工程。

 

簡易留言簿系統-ASP.NET MVC (後台基本完成)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.