搜索

查看: 3070|回复: 11

[ASP.NET] ASP.NET MVC实现登录后跳转到原界面

[复制链接]
发表于 2023-5-4 11:34:10 | 显示全部楼层 |阅读模式
Editor 2023-5-4 11:34:10 3070 11 看全部
有这样的一个需求:提交表单,如果用户没有登录,就跳转到登录页,登录后,跳转到原先表单提交这个页面,而且需要保持提交表单界面的数据。
提交表单的页面是一个强类型视图页,如果不考虑需要保持提交表单界面的数据,可以先设计这样的一个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)
    [i]
}
在控制器中大致这么写:
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";
}
Index
@ViewBag.msg
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    @Html.TextBoxFor(m => m.Name)
    [i]
}
所以,面对本篇开始描述的需求,仅仅跳转是不够的,需要向某个视图传递Model,而其中的关键是:
1、从路由中获取action名称
2、action名称和view名称一致
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对知鸟论坛的支持。如果你想了解更多相关内容请查看下面相关链接
回复

使用道具 举报

发表于 2023-6-28 20:38:23 | 显示全部楼层
井底燕雀傥 2023-6-28 20:38:23 看全部
楼主太厉害了!楼主,I*老*虎*U!我觉得知鸟论坛真是个好地方!
回复

使用道具 举报

发表于 2023-6-28 22:41:50 | 显示全部楼层
123456833 2023-6-28 22:41:50 看全部
论坛不能没有像楼主这样的人才啊!我会一直支持知鸟论坛
回复

使用道具 举报

发表于 2023-6-29 00:59:57 | 显示全部楼层
永远就三年疗 2023-6-29 00:59:57 看全部
这个帖子不回对不起自己!我想我是一天也不能离开知鸟论坛
回复

使用道具 举报

发表于 2023-6-29 01:16:12 | 显示全部楼层
xinting_6ym 2023-6-29 01:16:12 看全部
楼主发贴辛苦了,谢谢楼主分享!我觉得知鸟论坛是注册对了!
回复

使用道具 举报

发表于 2023-6-29 04:14:02 | 显示全部楼层
胡37 2023-6-29 04:14:02 看全部
楼主太厉害了!楼主,I*老*虎*U!我觉得知鸟论坛真是个好地方!
回复

使用道具 举报

发表于 2023-6-29 14:09:19 | 显示全部楼层
塞翁364 2023-6-29 14:09:19 看全部
我看不错噢 谢谢楼主!知鸟论坛越来越好!
回复

使用道具 举报

发表于 2023-6-30 01:21:25 | 显示全部楼层
伊索谗言 2023-6-30 01:21:25 看全部
论坛不能没有像楼主这样的人才啊!我会一直支持知鸟论坛
回复

使用道具 举报

发表于 2023-6-30 09:07:15 | 显示全部楼层
永远爱你冰塘 2023-6-30 09:07:15 看全部
我看不错噢 谢谢楼主!知鸟论坛越来越好!
回复

使用道具 举报

发表于 2023-6-30 17:09:33 | 显示全部楼层
十二音阶囤 2023-6-30 17:09:33 看全部
楼主太厉害了!楼主,I*老*虎*U!我觉得知鸟论坛真是个好地方!
回复

使用道具 举报

  • 您可能感兴趣
点击右侧快捷回复 【请勿灌水】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 返回列表

RSS订阅| SiteMap| 小黑屋| 知鸟论坛
联系邮箱E-mail:zniao@foxmail.com
快速回复 返回顶部 返回列表