zoukankan      html  css  js  c++  java
  • 在ASP.NET MVC中实现登录后回到原先的界面

    有这样的一个需求:提交表单,如果用户没有登录,就跳转到登录页,登录后,跳转到原先表单提交这个页面,而且需要保持提交表单界面的数据。

     

    提交表单的页面是一个强类型视图页,如果不考虑需要保持提交表单界面的数据,可以先设计这样的一个Model:

    public class Student
    
    {
    
        public string Name{get;set;}
    
        public string ReturnUrl{get;set;}
    
    }

    在提交表单的视图页,大致这么写:

    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    
    {
    
        @Html.Hidden("ReturnUrl", Request.Url.PathAndQuery)
    
        @Html.TextBoxFor(m => m.Name)
    
        <input type="submit" value="提交"/>
    
    }

    在控制器中大致这么写:

     

    public ActionResult Index()
    
    {
    
        return View(new Student());
    
    }
    
    [HttpPost]
    
    public ActionResult Index(Student student)
    
    {
    
        return Redirect(student.ReturnUrl);
    
    }
    

     

    可是,虽然回到了表单提交的强类型视图页,表单数据却没有得以保持。

     

    于是,想到了使用如下方式:

    return View("someview", somemodel);

     

    someview的名称如何获取呢?

     

    public ActionResult Index()
    
    {
    
        return View(new Student());
    
    }

    以上,如果我们获取到action的名称就相当于获取到视图的名称!

     

    重新设计Model:

        public class Student
    
        {
    
            public string Name { get; set; }
    
            public string ControllerName { get; set; }
    
            public string ActionName { get; set; }
    
        }

    可以先从路由中把action名称拿到,然后赋值给Student的ActionName属性。

     

        public class HomeController : Controller
    
        {
    
            
    
            public ActionResult Index()
    
            {
    
                Student student = new Student()
    
                {
    
                    ActionName = this.ControllerContext.RouteData.Values["action"].ToString(),
    
                    ControllerName = this.ControllerContext.RouteData.Values["controller"].ToString()
    
                };
    
                return View(student);
    
            }
    
            [HttpPost]
    
            public ActionResult Index(Student student)
    
            {
    
                ViewBag.msg = "我又回来了~~";
    
                //如果是登录,先验证,验证成功执行下面的代码
    
                return View(student.ActionName, student);
    
            }
    
        }    
    

    以上,student.ActionName值既是action名称也是view名称。

     

    在提交表单的强类型视图页:

     

    @model MvcApplication1.Models.Student
    
    @{
    
        ViewBag.Title = "Index";
    
        Layout = "~/Views/Shared/_Layout.cshtml";
    
    }
    
    <h2>Index</h2>
    
    <div>@ViewBag.msg</div>
    
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    
    {
    
        @Html.TextBoxFor(m => m.Name)
    
        <input type="submit" value="提交"/>
    
    }
    

     

    所以,面对本篇开始描述的需求,仅仅跳转是不够的,需要向某个视图传递Model,而其中的关键是:
    1、从路由中获取action名称
    2、action名称和view名称一致 

  • 相关阅读:
    15、事例十五:纹理映射
    14、事例十四:材质:十二个材质球
    [luogu]P1084 疫情控制
    [luogu]P2502 [HAOI2006]旅行
    [luogu]P2053 [SCOI2007]修车
    线性相关/线性基
    [luogu]P3629 [APIO2010]巡逻
    [luogu]P3623 [APIO2008]免费道路
    权值线段树
    树链剖分
  • 原文地址:https://www.cnblogs.com/darrenji/p/4246910.html
Copyright © 2011-2022 走看看