日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET MVC3 异步刷新

發(fā)布時(shí)間:2023/12/19 asp.net 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET MVC3 异步刷新 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
ASP.NET MVC3 異步刷新 2011-08-13 09:51:53 標(biāo)簽:MVC3?異步刷新?休閑?N Layer?職場 原創(chuàng)作品,允許轉(zhuǎn)載,轉(zhuǎn)載時(shí)請務(wù)必以超鏈接形式標(biāo)明文章?原始出處?、作者信息和本聲明。否則將追究法律責(zé)任。http://leelei.blog.51cto.com/856755/638408 好久沒寫博客了,今天就講講如何在ASP.NET MVC3中實(shí)現(xiàn)異步刷新。本次實(shí)例前臺(tái)采用的是Razor引擎。首先看看系統(tǒng)結(jié)構(gòu),如圖 本系統(tǒng)采用N Layer開發(fā)。我們以其中的某個(gè)具體模塊作為案例來講述。 @model TeacherPlatForm.Utility.PagedList<TeacherPlatForm.Domain.Entities.Generate.TASE_Thesis_Titles>?
<!DOCTYPE html>?
<html>?
<head>?
????????<title>論文題目發(fā)布</title>?
????????<link?rel="Stylesheet"?type="text/css"?href="http://www.cnblogs.com/Content/Site.css"?/>?
????????<script?type="text/javascript"?src="http://www.cnblogs.com/Scripts/jquery-1.4.4.js"></script>?
????????<script?type="text/javascript"?src="http://www.cnblogs.com/Scripts/jquery.unobtrusive-ajax.min.js"></script>?
????????<link?rel="Stylesheet"?type="text/css"?href="http://www.cnblogs.com/Scripts/jquery.autocomplete.css"?/>?
????????<script?type="text/javascript"?src="http://www.cnblogs.com/Scripts/jquery.autocomplete.js"></script>?
????????<script?type="text/javascript"?src="http://www.cnblogs.com/Scripts/common.js"></script>?
????????<script?type="text/javascript"?src="http://www.cnblogs.com/Scripts/teacherplat/thesistitlepublish.js"></script>?
</head>?
<body>?
????????<div?align="center">?
????????????????<div?align="left"?style="width: 96%; margin-top: 20px">?
????????????????????????當(dāng)前位置:論文題目發(fā)布?
????????????????????????<br?/>?
????????????????????????<hr?align="left"?noshade="noshade"?size="0.1"?color="#b70005"?/>?
????????????????</div>?
????????????????<input?type="hidden"?id="hfd_professional"?value="@ViewBag.professional"?/>?
????????????????<input?type="hidden"?id="hfd_educationLevel"?value="@ViewBag.educationId"?/>?
????????????????<input?type="hidden"?id="hfd_write_batchid"?name="hfd_write_batchid"?value="@ViewBag.writeBatchid"?/>?
????????????????@using (Ajax.BeginForm("ThesisOperation", "ThesisTitlePublish", new AjaxOptions { OnSuccess = "opersuccess()" }))?
????????????????{?
????????????????????????Html.RenderPartial("~/Views/TeacherPlatForm/ThesisTitlePartial.cshtml");?
????????????????????????<input?type="hidden"?id="hfd_sign"?name="hfd_sign"?/>?
????????????????????????<div>?
????????????????????????????????<input type="button" id="btnAdd" name="btnAdd" value="增加" class="btn" style="width: 60px"?
???????????????????????????????????????? />??
????????????????????????????????<input type="button" id="btnRemove" name="btnRemove" value="刪除" class="btn" style="width: 60px"?
???????????????????????????????????????? />??
????????????????????????????????<input type="button" id="btnPublish" name="btnPublish" value="發(fā)布" class="btn" style="width: 60px"?
???????????????????????????????????????? />?
????????????????????????</div>?
????????????????}?
????????</div>?
</body>?
</html> 在本頁面我引用了一個(gè)partial頁面代碼如下 @model TeacherPlatForm.Utility.PagedList<TeacherPlatForm.Domain.Entities.Generate.TASE_Thesis_Titles>?
<div?id="thesisTitle_tab">?
????????@Html.Hidden("thesisTitleIds", (Model != null ? string.Join("_", Model.DataList.Select(tt => tt.thesis_title_id)) : string.Empty))?
????????<table?class="mytable"?style="margin-top: 15px">?
????????????????<tr>?
????????????????????????<th>?
????????????????????????????????<center>?
????????????????????????????????????????@Html.CheckBox("chk_all", new { id = "chkall", onclick = "checkall()" })?
????????????????????????????????</center>?
????????????????????????</th>?
????????????????????????<th>?
????????????????????????????????<center>?
????????????????????????????????????????題目</center>?
????????????????????????</th>?
????????????????????????<th>?
????????????????????????????????<center>?
????????????????????????????????????????寫作批次</center>?
????????????????????????</th>?
????????????????????????<th>?
????????????????????????????????<center>?
????????????????????????????????????????專業(yè)名稱</center>?
????????????????????????</th>?
????????????????????????<th>?
????????????????????????????????<center>?
????????????????????????????????????????方向?
????????????????????????????????</center>?
????????????????????????</th>?
????????????????????????<th>?
????????????????????????????????<center>?
????????????????????????????????????????發(fā)布狀態(tài)</center>?
????????????????????????</th>?
????????????????????????<th>?
????????????????????????????????<center>?
????????????????????????????????????????操作?
????????????????????????????????</center>?
????????????????????????</th>?
????????????????</tr>?
????????????????@if (Model != null && Model.DataList.Count > 0)?
????????????????{?
????????????????????????foreach (var thesisTitle in Model.DataList)?
????????????????????????{?
????????????????????????<tr?onclick="setCheck('@thesisTitle.thesis_title_id')">?
????????????????????????????????<td?align="center">?
????????????????????????????????????????@Html.CheckBox("chk_" + thesisTitle.thesis_title_id, new { id = "chk_" + thesisTitle.thesis_title_id, onclick = "setCheck('" + thesisTitle.thesis_title_id + "')" })?
????????????????????????????????</td>?
????????????????????????????????<td>?
????????????????????????????????????????@thesisTitle.title?
????????????????????????????????</td>?
????????????????????????????????<td>?
????????????????????????????????????????@(thesisTitle.TASE_Write_Batch != null ? thesisTitle.TASE_Write_Batch.write_batch_name : string.Empty)?
????????????????????????????????</td>?
????????????????????????????????<td>?
????????????????????????????????????????@(thesisTitle.TASE_Thesis_Direction != null ? (thesisTitle.TASE_Thesis_Direction.TASE_Professional != null ? thesisTitle.TASE_Thesis_Direction.TASE_Professional.name : string.Empty) : string.Empty)?
????????????????????????????????</td>?
????????????????????????????????<td>?
????????????????????????????????????????@(thesisTitle.TASE_Thesis_Direction != null ? thesisTitle.TASE_Thesis_Direction.direct_name : string.Empty)?
????????????????????????????????</td>?
????????????????????????????????<td>?
????????????????????????????????????????@(thesisTitle.state == "1" ? "已發(fā)布" : "未發(fā)布")?
????????????????????????????????</td>?
????????????????????????????????<td?align="center">?
????????????????????????????????????????<input?type="button"?id="btnModify"?name="btnModify"?class="btn"?value="修改"?style="width:60px"?onclick="showModifyModal('@thesisTitle.thesis_title_id')"?/>?
????????????????????????????????</td>?
????????????????????????</tr>?}?
????????????????}?
????????</table>?
????????<div?class="divpager">?
????????????????共有?<font?color="red">@(Model != null ? Model.TotalCount : 0)</font>?條記錄 當(dāng)前是第?<font?color="red">@(Model != null ? Model.PageIndex : 0)</font>?
????????????????頁 共<font?color="red">@(Model != null ? Model.TotalPages : 0)</font>頁?????
????????????????@if (Model!=null&&Model.HasPreviousPage)?
????????????????{?
????????????????????????@Ajax.ActionLink("首頁", "Index", "ThesisTitlePublish", new { id = 1 + "_" + 10 + "_" + DateTime.Now.Ticks }, new AjaxOptions { UpdateTargetId = "thesisTitle_tab" })<label>?</label>?
????????????????????????@Ajax.ActionLink("上一頁", "Index", "ThesisTitlePublish", new { id = (Model.PageIndex - 1) + "_" + 10 + "_" + DateTime.Now.Ticks }, new AjaxOptions { UpdateTargetId = "thesisTitle_tab" })<label>?</label>????
????????????????}?
????????????????else?
????????????????{?
????????????????????????<a>首頁?</a>????
????????????????????????<a>上一頁?</a>?
????????????????}?
????????????????@if (Model != null && Model.HasNextPage)?
????????????????{?
????????????????????????@Ajax.ActionLink("下一頁", "Index", "ThesisTitlePublish", new { id = (Model.PageIndex + 1) + "_" + 10 + "_" + DateTime.Now.Ticks }, new AjaxOptions { UpdateTargetId = "thesisTitle_tab" })<label>?</label>?
????????????????????????@Ajax.ActionLink("末頁", "Index", "ThesisTitlePublish", new { id = Model.TotalPages + "_" + 10 + "_" + DateTime.Now.Ticks }, new AjaxOptions { UpdateTargetId = "thesisTitle_tab" })?
????????????????}?
????????????????else?
????????????????{?
????????????????????????<a>下一頁?</a>????
????????????????????????<a>末頁?</a>?
????????????????}?
????????</div>?
</div> 現(xiàn)在我對(duì)分頁采用了異步刷新,后臺(tái)代碼如下 ?public?ActionResult Index(string?id)?
????????????????{?
????????????????????????try?
????????????????????????{?
????????????????????????????????int?pageSize = 10, pageIndex = 1;?
????????????????????????????????if?(!string.IsNullOrEmpty(id))?
????????????????????????????????{?
????????????????????????????????????????pageIndex =?int.Parse(id.Split('_')[0]);?
????????????????????????????????????????pageSize =?int.Parse(id.Split('_')[1]);?
????????????????????????????????}?
?????????????????????????????????
????????????????????????????????TASE_Write_Batch writeBatch = thesisTitlePublishService.GetWriteBatch();?
????????????????????????????????PagedList<TASE_Thesis_Titles> thesisTitleList = thesisTitlePublishService.GetThesisTitleList((string)Session["userid"], writeBatch.write_batch_id, pageIndex, pageSize);?
????????????????????????????????if?(Request.IsAjaxRequest())?
????????????????????????????????{?
????????????????????????????????????????return?PartialView("~/Views/TeacherPlatForm/ThesisTitlePartial.cshtml", thesisTitleList);?
????????????????????????????????}?
????????????????????????????????return?View("~/Views/TeacherPlatForm/ThesisTitlePublish.cshtml", thesisTitleList);?
????????????????????????}?
????????????????????????catch?(Exception ex)?
????????????????????????{?
????????????????????????????????if?(ex.GetType() ==?typeof(MyException))?
????????????????????????????????{?
????????????????????????????????????????ViewBag.errorMessage = ((MyException)(ex)).Message;?
????????????????????????????????????????return?View("~/Views/TeacherPlatForm/ErrorMessage.cshtml");?
????????????????????????????????}?
????????????????????????????????else?
????????????????????????????????{?
????????????????????????????????????????return?View("~/Views/TeacherPlatForm/ThesisTitlePublish.cshtml",?null);?
????????????????????????????????}?
????????????????????????}?
????????????????} 在后臺(tái)控制器判斷如果是AJAX請求,返回一個(gè)Partial頁面。去替換<div id="thesisTitle_tab">中的內(nèi)容。怎么替換呢,看看partial頁中的分頁代碼,@Ajax.ActionLink("首頁", "Index", "ThesisTitlePublish", new { id = 1 + "_" + 10 + "_" + DateTime.Now.Ticks }, new AjaxOptions { UpdateTargetId = "thesisTitle_tab" })看見AjaxOptions有一個(gè)UpdateTargetId屬性,這就是我們分頁后要刷新的html標(biāo)簽的id。 對(duì)于增加刪除我們也可以采用類似的方式,不過UpdateTargetId 要寫在@using (Ajax.BeginForm("ThesisOperation", "ThesisTitlePublish", new AjaxOptions { OnSuccess = "opersuccess()" })) ? 中。這里我們采用了整頁刷新 public?JavaScriptResult RemoveThesisTitle(FormCollection fc)?
????????????????{?
????????????????????????try?
????????????????????????{?
????????????????????????????????string?thesisTitleId = fc["thesisTitleIds"];?
????????????????????????????????if?(thesisTitleId.Length == 0)?
????????????????????????????????{?
????????????????????????????????????????return?JavaScript("alert('請選擇要?jiǎng)h除的題目!')");?
????????????????????????????????}?
????????????????????????????????string[] thesisTitleIds = thesisTitleId.Split(new?char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);?
????????????????????????????????foreach?(var thesisTitleid?in?thesisTitleIds)?
????????????????????????????????{?
????????????????????????????????????????string?isChecked = fc["chk_"?+ thesisTitleid].Split(',')[0];?
????????????????????????????????????????if?(isChecked.Equals("true"))?
????????????????????????????????????????{?
????????????????????????????????????????????????TASE_Thesis_Titles thesisTitles = thesisTitlePublishService.GetThesisTitles(thesisTitleid);?
????????????????????????????????????????????????if?(thesisTitles.state ==?"1")?
????????????????????????????????????????????????{?
????????????????????????????????????????????????????????return?JavaScript("alert('已發(fā)布的論文題目不能刪除!')");?
????????????????????????????????????????????????}?
????????????????????????????????????????????????thesisTitlePublishService.RemoveThesisTitle(thesisTitles);?
????????????????????????????????????????}?
????????????????????????????????}?
????????????????????????????????thesisTitlePublishService.Commit();?
????????????????????????????????return?JavaScript("alert('刪除成功!');window.location.href=window.location.href;");?
????????????????????????}?
????????????????????????catch?
????????????????????????{?
????????????????????????????????return?JavaScript("alert('刪除失敗!')");?
????????????????????????}?
????????????????}

本文出自 “微軟技術(shù)” 博客,請務(wù)必保留此出處http://leelei.blog.51cto.com/856755/638408

轉(zhuǎn)載于:https://www.cnblogs.com/ppcompany/articles/2674634.html

總結(jié)

以上是生活随笔為你收集整理的ASP.NET MVC3 异步刷新的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。