学习做留言板

 

前不久做的那个“超然搜索”虽然是基于MVC制作,但是他复杂的地方是在搜索结果分析,MVC的很多特性没有用到,这几天好好学习了一下,做了一个简单的留言本,呵呵。演示地址依旧是www.chaoran44.com

程序所有的类图如下:

留言板模型验证代码

 

代码
1 [Bind(Include = "Name,Context")]
2 [MetadataType(typeof(Shout_Validation))]
3 public partial class Shout
4 {
5
6 }
7 public partial class Shout_Validation
8 {
9 [Required(ErrorMessage = "名字必须输入")]
10 [DisplayName("名字")]
11 [StringLength(50, ErrorMessage = "名字长度不能超过50个字符")]
12 public string Name { get; set; }
13
14 [Required(ErrorMessage = "内容必须输入")]
15 [DisplayName("内容")]
16 public string Context { get; set; }
17 }

 

 

控制器代码

 

代码
1 public class ShoutController : Controller
2 {
3 //
4 // GET: /Shout/
5  
6 public ActionResult Index()
7 {
8 Models.ShoutModels.ShoutRepositorycs SR = new Models.ShoutModels.ShoutRepositorycs();
9 return View(SR.GetShoutList());
10 }
11
12 [HttpGet ]
13 public ActionResult CreateShout()
14 {
15 return View(new Shout() { Name ="",Context=""});
16 }
17
18 [HttpPost]
19 public ActionResult CreateShout(Shout shout)
20 {
21 if (ModelState.IsValid == false)
22 {
23 ModelState.AddModelError("submit", "请正确填写后提交");
24 return View();
25 }
26
27 Models.ShoutModels.ShoutRepositorycs SR = new Models.ShoutModels.ShoutRepositorycs();
28 shout.IP = Request.UserHostAddress;
29 SR.SaveShout(shout);
30
31 return View("SingleShout", shout);
32 }
33
34 }

 

 

留言板采用部分视图显示,包括每条留言均使用部分视图显示。

调用留言板的页面代码

代码
1 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
2
3  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4  <html xmlns="http://www.w3.org/1999/xhtml">
5  <head runat="server">
6 <title>超然搜索</title>
7  </head>
8  <body>
9 <script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
10 <script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
11 <div>
12 <% Html.RenderAction("SearchKey", new { id = ViewData["id"] }); %>
13 </div>
14  </body>
15  </html>

 

 

显示留言列表

 

代码
1 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<CRSearch.Models.Shout>>" %>
2  <% foreach (var item in Model)
3 { %>
4  <% Html.RenderPartial("SingleShout", item); %>
5  <% } %>
6  

 

 

单条留言显示视图

 

代码
1 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CRSearch.Models.Shout>" %>
2  <fieldset>
3 <legend>
4 <%: Model.Name %>(<%: Model.IP %>)&nbsp;<%: String.Format("{0:g}", Model.CreateTime) %></legend>
5 <div class="display-field">
6 <%: Model.Context %></div>
7  </fieldset>

 

 

提交留言采用了Ajax,MVC的Ajax微软封装的我什么都不用做了,不知道这是好事还是坏事。

 

代码
1 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CRSearch.Models.Shout>" %>
2  <script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
3 <script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
4 <% using (Ajax.BeginForm("CreateShout", "Shout", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "CShout", LoadingElementId = "CShoutLoading" }))
5 {%>
6 <div id="CShout">
7 <%: Html.ValidationSummary(true)%>
8 <div class="editor-label">
9 <%: Html.LabelFor(model => model.Name)%>
10 </div>
11 <div class="editor-field">
12 <%: Html.TextBoxFor(model => model.Name)%>
13 <%: Html.ValidationMessageFor(model => model.Name)%>
14 </div>
15 <div class="editor-label">
16 <%: Html.LabelFor(model => model.Context)%>
17 </div>
18 <div class="editor-field">
19 <%: Html.TextAreaFor(model => model.Context, new { rows = 4, @class = "key" })%>
20 <%: Html.ValidationMessageFor(model => model.Context)%>
21 </div>
22 <p>
23 <input type="submit" value="留言" /><%: Html.ValidationMessage("submit") %>
24 <div id="CShoutLoading" style="display: none">提交中...</div>
25 </p>
26 </div>
27 <% } %>
28

 

 

 

posted @ 2011-01-23 16:10  超然  阅读(616)  评论(3编辑  收藏  举报