zoukankan      html  css  js  c++  java
  • 简易留言簿系统ASP.NET MVC (后台基本完成)

    今天在工作的空余,完成了简易留言簿系统的基本功能。在这里总结全面总结一下。

    简易留言薄系统: 开发工具: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没有了服务器端控件,使得开发的灵活度更大了。

    简易留言簿系统的功能基本实现:会员注册,登录。留言的发布,删除,回复留言的发布,删除。但是网站的安全,注册发送确认邮件,前台设计等都还没有实现。

    接下来的需要做的是前台页面的设计,同时进行下一个网站(互动媒体学习社区)的工程。

  • 相关阅读:
    oracle 导入数据时提示只有 DBA 才能导入由其他 DBA 导出的文件
    oracle 常用语句
    android udp 无法收到数据 (模拟器中)
    android DatagramSocket send 发送数据出错
    AtCoder ABC 128E Roadwork
    AtCoder ABC 128D equeue
    AtCoder ABC 127F Absolute Minima
    AtCoder ABC 127E Cell Distance
    CodeForces 1166E The LCMs Must be Large
    CodeForces 1166D Cute Sequences
  • 原文地址:https://www.cnblogs.com/Weimin496/p/7422347.html
Copyright © 2011-2022 走看看