一、BaseController.cs文件

1.OnActionExecuting方法,该方法可以被各子Controller重写

 protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
//do this in OnActionExecuting instead of constructor to
//A) make sure the child class has fully initialized and
//B) that there is a valid request in context // Apply a default title for the page, can be overridden in view or controller
ViewBag.Title = ControllerFriendlyName;
ViewBag.StudentSwitcherReturnUrl = GetStudentSwitcherReturnUrl();
}

2.GetStudentSwitcherReturnUrl方法,该方法可以被各子Controller重写

 protected virtual string GetStudentSwitcherReturnUrl()
{
return null;
}

二、后台Controller代码

1.如果一个Controller中只有一个Action需要在切换学生时满足该操作

例:ChangePassword页面

 protected override string GetStudentSwitcherReturnUrl()
{
return Url.Action<ChangePasswordController>(c => c.Index(false));
}

2.如果一个Controller中只有多个Action需要在切换学生时满足该操作

例:StudentAddress和Index,这两个Action都在ContactController中。在这种情况下就需要根据请求的Action名字进行判断。

  protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
ViewBag.StudentSwitcherReturnUrl = filterContext.ActionDescriptor.ActionName == "StudentAddress" ?
Url.Action<ContactController>(c => c.StudentAddress()) : Url.Action<ContactController>(c => c.Index());
}

三、前台页面代码

......
......
//获取点击学生时需要跳转的URL即需要停留的当前页面的URL
string studentSwitcherReturnUrl = ViewBag.StudentSwitcherReturnUrl;
......
......
//包含该URL的超链接
@(Html.ActionLink<StudentSwitcherController>(studentName, c => c.Index(student.Id, studentSwitcherReturnUrl)))

四、实现思路

1.切换学生时页面还停留在当前页的本质:实质上是又进行了一次跳转,跳转到了当前页面!

2.由于每个Controller都继承自BaseController,因此在BaseController中定义ViewBag.StudentSwitcherReturnUrl,这样每个页面都可以使用StudentSwitcherReturnUrl。

3.在具体的每个需要操作的Controller中给StudentSwitcherReturnUrl赋值,赋值为具体的Link字符串。

4.这样在每个Controller对应的页面中就可以取得StudentSwitcherReturnUrl的值,再使用它转配成具体的超链接即可。

5.OnActionExcuting方法:在调用该Action之前调用该方法,执行其中的操作。

6.ActionExecutingContext类:提供 ActionFilterAttribute类的 ActionExecuting 方法的上下文。

它派生自ControllerContext,可以通过它获得该Action的信息。详见https://msdn.microsoft.com/zh-cn/library/system.web.mvc.actionexecutingcontext(v=vs.118).aspx

Portal系统中当切换学生时仍旧停留在当前页面的实现方法的更多相关文章

  1. Linux 系统中用户切换

    1. Linux系统中用户切换的命令为su,语法为: su [-fmp] [-c command] [-s shell] [--help] [--version] [-] [USER [ARG]] 参 ...

  2. Linux 系统中用户切换(su user与 su - user 的区别)

    1. Linux系统中用户切换的命令为su,语法为: su [-fmp] [-c command] [-s shell] [--help] [--version] [-] [USER [ARG]] 参 ...

  3. linux系统中用户切换

    1. Linux系统中用户切换的命令为su,语法为: su [-fmp] [-c command] [-s shell] [--help] [--version] [-] [USER [ARG]] 参 ...

  4. 在OS X系统中php访问sftp时需要ssh2扩展的安装

    php -v brew install homebrew/php/php55-ssh2 [实现方式] <?php $connection = ssh2_connect('192.168.0.14 ...

  5. vue中使用vue-router切换页面时滚动条自动滚动到顶部的方法

    原文:http://www.jb51.net/article/129270.htm main.js入口文件配合vue-router写这个 router.afterEach((to,from,next) ...

  6. Titanium中调用ios组件时语言不是本地化的解决方法

    用Titanium开发的ios应用中,当调用系统组件时,尽管手机已经设置了系统语言为中文,但那些组件的界面却仍为英文.比如调用iphone中的相册组件,其界面为: 那么怎么让它跟系统语言保持一致呢? ...

  7. 织梦系统中出现DedeTag Engine Create File False提示原因及解决方法

    今天更新网站时dedecms系统时,遇到一个问题:DedeTag Engine Create File False  出现这样的提示. 其实这也不算是什么错误,我个人觉得最重要的一点就是根目录下没有给 ...

  8. Android中的EditText默认时不弹出软键盘的方法

    方法一: 在 AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为 adjustUnspecified|stateHidden <a ...

  9. 64位win10系统中无法开启vmware的VT-X嵌套虚拟化功能的解决方法

    在升级了win10操作系统之后,发现Vmware Workstation在安装64位操作系统虚拟机的或者要使用Intel VT-X/EPT的时候,会一直弹出vt-x被禁用的提示,如下图:       ...

随机推荐

  1. php -- 文件操作(创建、复制、移动、删除)

    创建 文件夹 bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $con ...

  2. 数据驱动ddt

    在设计用例的时候,有些用例操作过程是一样的,只是参数数据输入的不同,如果用例重复的去写操作过程会增加代码量,对于这种多组数据的测试用例,可以使用数据驱动设计模式,一组数据对应一个测试用例,用例自动加载 ...

  3. Struts2_day01--导入源文件_Struts2的执行过程_查看源代码

    导入源文件 选中按ctrl + shift + t进入 Struts2执行过程 画图分析过程 过滤器在服务器启动时创建,servlet在第一次访问时创建 查看源代码 public class Stru ...

  4. 苹果发布【新开发语言】Swift

    Swift is a multi-tenant, highly scalable and durable object storage system that was designed to stor ...

  5. Python 正则表达式贪婪模式

    贪婪模式也就是我们使用 .* 匹配任意字符时会尽可能长地向后匹配,如果我们想阻止这种贪婪模式,需要加个问号,尽可能少地匹配,如下例子: In []: import re In []: html = ' ...

  6. vue的递归组件以及三级菜单的制作

    js里面有递归算法,同时,我们也可以利用props来实现vue模板的递归调用,但是前提是组件拥有 name 属性 父组件:slotDemo.vue: <template> <div& ...

  7. JSP内置对象——session

    sessionsession表示客户端与服务器的一次会话Web中的session指的是用户在浏览某个网站时,从进入网站到浏览器关闭所进过的这段时间,也就是用户浏览这个网站所花费的时间从上述定义中可以看 ...

  8. flex布局,input点击软键盘激活,底部按钮上移

    安卓手机的问题,原本表单没有超出一屏的时候,上移的现象也是存才的,只是需要滑动body才能看得到,现在,超出一屏了,现象就很明显了 body明显上移: 点击input修改价格: 软键盘出现: 先尝试了 ...

  9. 数据集划分——train set, validate set and test set

    先扯点闲篇儿,直取干货者,可以点击这里. 我曾误打误撞的搞过一年多的量化交易,期间尝试过做价格和涨跌的预测,当时全凭一腔热血,拿到行情数据就迫不及待地开始测试各种算法. 最基本的算法是技术指标类型的, ...

  10. nginx的简单使用和使用nginx在windows上搭建tomcat集群

    nginx是一款轻量级的web服务器,常用的作用为服务器/反向代理服务器以及电子邮件(IMAP/POP3)代理服务器 1.为什么我们要使用Nginx? 反向代理: 反向代理(Reverse Proxy ...