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,一经查实,立即删除!

相关文章

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

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

检测APK是否混淆

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

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

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

SkipList 以及高度的确定

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

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 传输控制协议)是一种面向连接(连接导向)的、可靠的、 …

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; 原子性是指事务包含…

Paypal 在线支付接口应用从零开始,第2节,[支付API原理及流程]

今天看看Paypal支付流程和简单的认证原理,我画了一张图.应该能表达这两点意思了我们的站点名字,为了好理解,暂且就定为西狐的网站吧.点此查看清晰原图恩,理论知识很重要哈,先把这图理解了,下一步我们编程使用沙盒测试就很简单了.如果想更多研究一下还可直接查看Paypal官方提供的…

PLSQL DEVELOPER 使用技巧

为什么80%的码农都做不了架构师&#xff1f;>>> 1、右键菜单 在PL/SQL Developer&#xff08;下面简称PLD&#xff09;中的每一个文本编辑窗口&#xff0c;如SQL Window&#xff0c;Command Window和Porgram Window&#xff0c;右键点击某个对象名称&#xff0c;会…

Android Drawable绘图学习笔记

如何获取 res 中的资源 数据包package&#xff1a;android.content.res 主要类&#xff1a;ResourcesAndroid SDK中的简介&#xff1a;Class for accessing an application’s resources.Class for accessing an application’s resources. This sits on top of the asset mana…

C#.NET中的事件2

/** Created by SharpDevelop. * User: noo * Date: 2009-8-17 * Time: 15:34 * * 事件2 */usingSystem ;usingSystem .Windows .Forms ;classTest { staticvoidMain()//入口函数{ Form frmnewForm ();//新建一窗体frm.Text "我的窗体"; …

HTML学习之基础

HTML是网页的标记语言不是编程语言&#xff0c;有一些标记段组成。大小写不敏感&#xff0c;可以用常用的编辑器软件编写用浏览器打开即可 有不同的版本<!DOCTYPE html> <meta charset"utf-8">在<title>前为了能正常显示中文。 超链接&#xff1a;…

Varint

什么是Varint Varint 是一种紧凑的表示数字的方法。它用一个或多个字节来表示一个数字&#xff0c;值越小的数字使用越少的字节数。这能减少用来表示数字的字节数。 比如对于 int32 类型的数字&#xff0c;一般需要 4 个 byte 来表示。但是采用 Varint&#xff0c;对于很小的 …

Ubuntu 17.10安装Qt 5.10环境与Qt Creator 4.5开发工具(转自linux公社)

记录下在Ubuntu 17.10搭建Qt环境与安装Qt Creator开发工具的过程。机器装的Linux是Ubuntu 17.10&#xff0c;16.04与17.04的应该也相同。Qt 5.10和Qt 3D Studio发布 http://www.linuxidc.com/Linux/2017-12/149267.htm 1&#xff0c;Qt安装 1.1 下载Qt 这里提供Qt Creator的下…

C#基础(201)--常量枚举

本文知识点&#xff1a; 1.掌握常量的定义和使用方法 2.理解枚举的作用和特点 3.掌握枚举的使用方法 1.1.常量的定义语法 const 数据类型 常量名称 值&#xff1b; 1.2.常见错误 1.3常量的使用时机 经常使用并且值不变的变量&#xff0c;可以定义为常量 2.1枚举的作用及其…

unbuntu使用经典界面

为什么80%的码农都做不了架构师&#xff1f;>>> 昨天升级到UBUNTU 11.04, 发现新的Unity界面很不适应&#xff0c;于是将其恢复到旧式经典界面&#xff0c;具体操作模式方法如下&#xff1a; 在已经登录的状态下&#xff0c;选择 [注销]然后在重新登录的时候&#…

c++入门基础知识

命名空间刚开始接触c&#xff0c;我们会发现与C语言相比不光头文件有所不同&#xff0c;还会发现using namespce std&#xff1b;这句话&#xff0c;其实这就是c的命名空间。 (1) 概念命名空间是为了防止名字冲突提供更加可控的机制。命名空间分割了全局命名空间&#xff0c;其…

读书笔记之《得未曾有》

作者 安妮宝贝&#xff0c;2014年笔名改为“庆山” 感想 第一次读庆山的作品&#xff0c;可以书名来总结的一下&#xff0c;得未曾有——获得了一种未曾有过得感受。 一、感受作者 高晓松老师的节目里说过一句话&#xff0c;写作需要长时间的观察人性、需要极强的观察能力。庆山…

linux驱动简单介绍

linux驱动简单介绍 驱动基本介绍 驱动。顾名思义就是“驱使硬件设备行动”。设备驱动与底层硬件之间打交道&#xff0c;按照硬件设备的具体操作方式来读写设备寄存器&#xff0c;最终完成一系列操作。 设备 驱动充当了应用程序和应用软件直接的纽带&#xff0c;它使得应用软件只…