AJAX基於ASP.NET MVC

iDotNetSpace發表於2008-08-26

AJAX基於ASP.NET MVC

看一下Controller中的對應的Action方法:

1 public void SearchUserAjax()
2 {
3 string uerid = Request["name"];
4
5 List<UserLunchList> lunchs = (
6 from userlunch in repository.UserLunchLists.ToList()
7 where userlunch.UserID == int.Parse(uerid)
8 select userlunch
9 ).ToList();
10 StringBuilder sb = new StringBuilder();
11 sb.Append("<table id='LunchList' style='display:none'><tr><th>User</th><th>Time</th><th>Price</th></tr>");
12 foreach (UserLunchList lunch in lunchs)
13 {
14 sb.Append("<tr><td>" + lunch.User.UserName + "</td><td>" + lunch.Time.ToShortDateString() + "</td><td>" + lunch.Cost + "</td></tr>");
15 }
16 sb.Append("</table>");
17 Response.ContentType = "text/html";
18 Response.Write(sb.ToString());
19 }


  說穿了很簡單,就是把想要的資料直接寫到Response中就可以了,這裡為了方便起見,就是寫好了Table的格式。有一個地方需要注意的就是這個語句

  Response.ContentType = "text/html" 很重要,它告知JavaScript以何種格式來處理Response中的資料。

  核心內容就這麼簡單,呵呵。

  如果覺得寫JavaScript程式碼煩的話,可以使用extension方法,自定一個Html.Form或者其它的控制元件。 .pb{} .pb textarea{font-size:14px; margin:10px; font-family:"宋體"; background:#FFFFEE; color:#000066} .pb_t{line-height:30px; font-size:14px; color:#000; text-align:center;} /* 分頁 */ .pagebox{overflow:hidden; zoom:1; font-size:12px; font-family:"宋體",sans-serif;} .pagebox span{float:left; margin-right:2px; overflow:hidden; text-align:center; background:#fff;} .pagebox span a{display:block; overflow:hidden; zoom:1; _float:left;} .pagebox span.pagebox_pre_nolink{border:1px #ddd solid; width:53px; height:21px; *height:23px; line-height:21px; text-align:center; color:#999; cursor:default;} .pagebox span.pagebox_pre{color:#3568b9; height:23px;} .pagebox span.pagebox_pre a,.pagebox span.pagebox_pre a:visited,.pagebox span.pagebox_next a,.pagebox span.pagebox_next a:visited{border:1px #9aafe5 solid; color:#3568b9; text-decoration:none; text-align:center; width:53px; cursor:pointer; height:21px; *height:23px; line-height:21px;} .pagebox span.pagebox_pre a:hover,.pagebox span.pagebox_pre a:active,.pagebox span.pagebox_next a:hover,.pagebox span.pagebox_next a:active{color:#363636; border:1px #2e6ab1 solid;} .pagebox span.pagebox_num_nonce{padding:0 8px; height:23px; line-height:23px; color:#fff; cursor:default; background:#296cb3; font-weight:bold;} .pagebox span.pagebox_num{color:#3568b9; height:23px;} .pagebox span.pagebox_num a,.pagebox span.pagebox_num a:visited{border:1px #9aafe5 solid; color:#3568b9; text-decoration:none; padding:0 8px; cursor:pointer; height:21px; *height:23px; line-height:21px;} .pagebox span.pagebox_num a:hover,.pagebox span.pagebox_num a:active{border:1px #2e6ab1 solid;color:#363636;} .pagebox span.pagebox_num_ellipsis{color:#393733; width:22px; background:none; line-height:23px;} .pagebox span.pagebox_next_nolink{border:1px #ddd solid; width:53px; height:21px; *height:23px; line-height:21px; text-align:center; color:#999; cursor:default;}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-434717/,如需轉載,請註明出處,否則將追究法律責任。

相關文章