zoukankan      html  css  js  c++  java
  • Portal系统中当切换学生时仍旧停留在当前页面的实现方法

    一、BaseController.cs文件

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

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

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

    1 protected virtual string GetStudentSwitcherReturnUrl()
    2 {
    3     return null;
    4 }

    二、后台Controller代码

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

    例:ChangePassword页面

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

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

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

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

    三、前台页面代码

    ......
    ......
    //获取点击学生时需要跳转的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

  • 相关阅读:
    Java设计模式6
    数据库的三范式是什么?
    Mysql 索引的原理和实现
    Java Calendar
    读书-《众病之王:癌症传》-癌症的历史
    Numpy的终极备忘录
    监督学习和kNN分类初学者教程
    用PyTorch实现图像聚类
    Pandas概论
    掌握Python字典的12个例子
  • 原文地址:https://www.cnblogs.com/sunshineground/p/4316104.html
Copyright © 2011-2022 走看看