ASP.NET MVC3 异步刷新

ASP.NET MVC3 异步刷新
2011-08-13 09:51:53
标签:MVC3 异步刷新 休闲 N Layer 职场
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://leelei.blog.51cto.com/856755/638408
好久没写博客了,今天就讲讲如何在ASP.NET MVC3中实现异步刷新。本次实例前台采用的是Razor引擎。首先看看系统结构,如图
本系统采用N Layer开发。我们以其中的某个具体模块作为案例来讲述。
@model TeacherPlatForm.Utility.PagedList<TeacherPlatForm.Domain.Entities.Generate.TASE_Thesis_Titles> 
<!DOCTYPE html> 
<html> 
<head> 
        <title>论文题目发布</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"> 
                        当前位置:论文题目发布 
                        <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="发布" class="btn" style="width: 60px" 
                                         /> 
                        </div> 
                } 
        </div> 
</body> 
</html>
在本页面我引用了一个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> 
                                        专业名称</center> 
                        </th> 
                        <th> 
                                <center> 
                                        方向 
                                </center> 
                        </th> 
                        <th> 
                                <center> 
                                        发布状态</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" ? "已发布" : "未发布") 
                                </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> 条记录 当前是第 <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>
现在我对分页采用了异步刷新,后台代码如下
 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); 
                                } 
                        } 
                }
在后台控制器判断如果是AJAX请求,返回一个Partial页面。去替换<div id="thesisTitle_tab">中的内容。怎么替换呢,看看partial页中的分页代码,@Ajax.ActionLink("首页", "Index", "ThesisTitlePublish", new { id = 1 + "_" + 10 + "_" + DateTime.Now.Ticks }, new AjaxOptions { UpdateTargetId = "thesisTitle_tab" })看见AjaxOptions有一个UpdateTargetId属性,这就是我们分页后要刷新的html标签的id。
对于增加删除我们也可以采用类似的方式,不过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('请选择要删除的题目!')"); 
                                } 
                                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('已发布的论文题目不能删除!')"); 
                                                } 
                                                thesisTitlePublishService.RemoveThesisTitle(thesisTitles); 
                                        } 
                                } 
                                thesisTitlePublishService.Commit(); 
                                return JavaScript("alert('删除成功!');window.location.href=window.location.href;"); 
                        } 
                        catch 
                        { 
                                return JavaScript("alert('删除失败!')"); 
                        } 
                }

本文出自 “微软技术” 博客,请务必保留此出处http://leelei.blog.51cto.com/856755/638408

转载于:https://www.cnblogs.com/ppcompany/articles/2674634.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/463364.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一个反射的妙用案例

今天在工作中遇到一种场景: 一个表格有10个列,要求当点击某列列头时,下拉展示该列的所有数据 解决的常规办法是根据传入的列参数值,判断是哪一列,使用switch()或者if-else判断过滤出哪一列的值 但这个办法太臃肿,如果有100个字段,那这个分支选择就太大了 所以采用反射,你传什么…

常规键盘快捷键

按键 目的 Ctrl A 全选Ctrl B 整理收藏夹Ctrl C 复制Ctrl D 删除/添加收藏Ctrl E 搜索助理Ctrl F 查找Ctrl H 历史记录Ctrl I 收藏夹Ctrl J IE7.0里的源Ctrl L &#xff1d;CtrlO 打开Ctrl M 网页搜索框中的确定&#xff08;等于回车&#xff09;Ctrl N 新窗…

指针和引用的区别和联系

在c的学习中&#xff0c;我们经常被应该如何使用指针和引用这个问题所困扰&#xff0c;那么我这里给大家说一下在我的学习过程中对指针和引用的理解&#xff0c;和它们之间的区别&#xff0c;希望各位朋友给出不同的意见并且指出我在学习中的一些错误首先我们来聊一下指针&…

编译rocksdb源码导致的部署失败

这几天经历了一次心酸的历程&#xff0c;使用了rocksdb第三方库&#xff0c;编译器是7.2&#xff0c;rocksdb是20190701从github上取下来的&#xff0c;由于rocksdb自己的CMakeList.txt中使用了-marchnative编译参数&#xff0c;强制使用了编译代码服务器的cpu指令集&#xff0…

跌倒的收获

有个"渔王"非常苦恼&#xff0c;就因为三个儿子的捕鱼技术都很一般。他常向人诉苦&#xff1a;"我从儿子们刚懂事起就传授技术&#xff0c;怎么他们都比不上平常人的孩子&#xff1f;" 一位路人听后&#xff0c;问&#xff1a;"你一直是手把手地教他们…

检测APK是否混淆

目录 为什么要检测APK是否混淆混淆的优点混淆的缺点APK的混淆的分类检测工具检测人工检测自动化检测为什么要检测APK是否混淆 apk代码混淆就是为了保护代码安全&#xff0c;防止被反编译拿到源码做审计找出危险漏洞&#xff0c;现在大部分的APK都会做混淆。 下面分析混淆APK后的…

基于asp.net的Web开发架构探索(转)

问题由来 最近在研究适合团队开发的web架构解决方案&#xff0c;该架构即要适合分工协作又要有一定扩展性&#xff0c;适合不同的数据库需要&#xff0c;因此我查阅了一些资料&#xff0c;初步构想出了一套架构&#xff0c;请各位多多指教。 探索 web开发架构最经典莫过于三层架…

ubuntu要更新18.04了,lei了lei了~~~

Ubuntu 18.04 LTS将于2018年4月26日发布。开发商现在有六个月的时间来打造“Bionic Beaver 仿生海狸”。 对于那些你不知道Ubuntu 4 &#xff08;xx.04&#xff09;版本遵循27周的时间表&#xff08;而不是10月发行的25周时间表&#xff0c;由于圣诞节和新年的小事情&#xff0…

SkipList 以及高度的确定

转载&#xff1a;https://www.cnblogs.com/lnlvinso/p/8848883.html 结果&#xff1a;skiplist的高度是个随机值。 SkipList理解 记下自己对跳表SkipList的理解。 SkipList采用空间换时间的思想&#xff0c;通过增加数据间的链接&#xff0c;达到加快查找速度的目的。 数据库L…

20个正则表达式,举一反三,相信对你很有用

转载:https://www.cnblogs.com/wxd0108/p/5580772.html 正则表达式经常被用于字段或任意字符串的校验&#xff0c;如下面这段校验基本日期格式的JavaScript代码&#xff1a; var reg /^(\\d{1,4})(-|\\/)(\\d{1,2})\\2(\\d{1,2})$/; var r fieldValue.match(reg); if(rnull)a…

约瑟夫环问题---循环单链表

约瑟夫环问题是比较经典的问题&#xff0c;原来做的题目是依次输出数字&#xff0c;而原来的循环链表结构不改变&#xff0c;今天遇到一道题是要求按照顺序重新组成一个循环单链表。 题目&#xff1a;一些人围坐一圈报数&#xff0c;形成一个循环单链表&#xff0c;当报数是m或…

IIS AppCreate子目录的错误(0x80020006)

这几天做了升级用的安装包,需要在原来的ASP 的虚拟目录下&#xff0c;再创建一个新的ASPNet 虚拟目录。上网查了C# iis设定的资料&#xff0c;按照上面一路做下来&#xff0c;还是无法成功过。 代码只有两行&#xff0c;如下&#xff1a;DirectoryEntry siteVDir new Director…

网络通信TCP协议三次握手

刚刚看linux公社看见里面一个讲TCP的文章&#xff0c;文章讲的很有意思生动形象&#xff0c;很有助于对TCP协议的理解和掌握&#xff0c;所以转载过来方便以后看一下HAHA~~~TCP是什么? TCP(Transmission Control Protocol 传输控制协议)是一种面向连接(连接导向)的、可靠的、 …

聊聊RocksDB Compact

导语 对于 LevelCompact 策略&#xff0c;RocksDB会根据每一层不同的策略计算出CompactScore&#xff0c;根据CompactScore大小来决定那一层将会优先进行Compact&#xff0c;然后选择Level-N 和Level-(N1&#xff09;的文件进行Compact。如何计算CompactScore&#xff1f; 如何…

Android入门逆引手册 - 12 评分条(RatingBar)的使用

这里介绍评分条android.widget.RatingBar类的使用。 ● 评分&#xff08;星&#xff09;的最大数的设置&#xff0c;调用setNumStars()方法。● 现在的评分的设置&#xff0c;调用setRating()方法。 例程源码(Java)[java]RatingBar ratingBar (RatingBar)findViewById(id.rati…

数据库事务的四大特性以及事务的隔离级别

本篇讲诉数据库中事务的四大特性&#xff08;ACID&#xff09;&#xff0c;并且将会详细地说明事务的隔离级别。 如果一个数据库声称支持事务的操作&#xff0c;那么该数据库必须要具备以下四个特性&#xff1a; ⑴ 原子性&#xff08;Atomicity&#xff09; 原子性是指事务包含…

程序员面试的一些注意点

一、前言 自己即将踏上求职的征程&#xff0c;所以整理一篇有关于程序员面试的小窍门&#xff0c;帮助自己和正在求职、即将求职的一些小伙伴跳过一些坑。 二、简历制作1. 不要放头像普通人大概率都不是帅哥美女&#xff0c;所以不想让外观成为评判标准的就不要放照片上去了。2…

如何解决数据倾斜问题?

转载&#xff1a;https://blog.csdn.net/Mr_HHH/article/details/89399518 今天在工作中遇到了数据倾斜的问题&#xff0c;一条SQL执行了8小时才执行完&#xff0c;看计划是先join再做distinct&#xff0c;卡在了join上&#xff0c;数据量比较大&#xff0c;并且重复数据比较多…

白盒测试不是测试,更不高级

测试不仅仅是软件概念&#xff0c;但白盒测试仅仅是软件概念。 上面这句话足以说明白盒测试不是测试&#xff0c;至少不等同于测试。 认为白盒测试更牛逼的一个常用论据是白盒测试需要对代码本身更高的熟悉程度&#xff0c;但说这样的话的人往往没有搞清楚测试究竟需要什么样的…

剑指Offer题解(Python版)

https://blog.csdn.net/tinkle181129/article/details/79326023# 二叉树的镜像 链表中环的入口结点 删除链表中重复的结点 从尾到头打印链表 斐波那契数列 跳台阶 变态跳台阶 矩形覆盖 把字符串转换成整数 平衡二叉树 和为S的连续正数序列 左…