BiaoBai View部分:
@using ChengJiDaoRuChaXun.Dao; @{ Layout = null; List<BiaoBai> biaobai = ViewBag.biaobai; } <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>表白墙</title> <link href="~/Content/biaobai/index.css" rel="stylesheet" /> <script src="~/Content/biaobai/jquery-1.7.2.min.js"></script> <script src="~/Content/biaobai/index.js"></script> <script type="text/javascript"> $(function () { $('#shuaxin').click(function () { window.location = window.location; }) }); </script> <style type="text/css"> #shuaxin { display: block; width: 120px; height: 65px; cursor: pointer; background-image:url('/Content/images/chongpai.png'); } #fabu { display: block; width: 120px; height: 65px; cursor: pointer; background-image: url('/Content/images/send.png'); } </style> </head> <body> <div id='top'> <span id='fabu' style="float:left" onclick="window.location.href = '/Function/FaBu'"> </span> <span id="shuaxin" style="float:left"></span> </div> <div id='main'> @if (biaobai != null) { Random r = new Random(); string c = "a"; foreach (BiaoBai item in biaobai) { c = "a" + (r.Next(5)+1);//循环随机数的产生,a1——a5 <dl class='paper @c'>@*a1——a5*@ <dt> <span class='username'>@item.Name</span> </dt> <dd class='content'>@item.NeiRong</dd> <dd class='bottom'> <span class='time'>@item.ShiJian</span> <a href="" class='close'></a> </dd> </dl> } } </div> <!--[if IE 6]> <script src="~/Content/biaobai/iepng.js"></script> <script type="text/javascript"> DD_belatedPNG.fix('#send,#close,.close'); </script> <![endif]--> </body> </html>
BiaoBai Controller部分:
JiKaoChengJiEntities1 db; public FunctionController() { db = new Dao.JiKaoChengJiEntities1(); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } // GET: Function public ActionResult Index() { return View(); } public ActionResult BiaoBai() { var q = from t in db.BiaoBai select t; var list = q.ToList(); ViewBag.biaobai = list; return View(); }
用户发布表白信息:
FaBu.View部分:
@model ChengJiDaoRuChaXun.Dao.BiaoBai @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>FaBu</title> <link href="~/Content/bootstrap.min.css" rel="stylesheet" /> <script src="~/Scripts/jquery-1.10.2.intellisense.js"></script> <script src="~/Scripts/bootstrap.min.js"></script> <style type="text/css"> #send-form { margin-left:10px; margin-right: 10px; } </style> </head> <body> <div id='send-form'> <p class='title'><span>写下你的表白</span><a href="" id='close'></a></p> <form action="/Function/FaBuPost" id="form1" method="post"> <p> <label for="username">昵称:</label> @Html.TextBoxFor(t => t.Name, new {@class= "form-control" }) @*<input type="text" class="form-control" id="exampleInputName2">*@ </p> <p> <label for="content">表白:</label> @Html.TextAreaFor(t => t.NeiRong, new {@class= "form-control"}) @*<textarea class="form-control" rows="3"></textarea>*@ </p> <div style="width:100%; margin:0 auto;"> <input type="submit" class="btn btn-default" value="提交" /> </div> </form> </div> </body> </html>
Controller部分:
[HttpPost] public ActionResult FaBuPost() { BiaoBai biaobai = new BiaoBai(); biaobai.ShiJian = DateTime.Now; //biaobai.NeiRong = Request.Form["content"]; db.BiaoBai.Add(biaobai); db.SaveChanges(); db.Dispose(); return RedirectToAction("BiaoBai", new { biaobai }); } public ActionResult FaBu(BiaoBai biaobai) { return View(); }