久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術文章
文章詳情頁

ASP.NET MVC實現登錄后跳轉到原界面

瀏覽:6日期:2022-06-08 10:43:48

有這樣的一個需求:提交表單,如果用戶沒有登錄,就跳轉到登錄頁,登錄后,跳轉到原先表單提交這個頁面,而且需要保持提交表單界面的數據。

提交表單的頁面是一個強類型視圖頁,如果不考慮需要保持提交表單界面的數據,可以先設計這樣的一個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名稱一致 

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對的支持。如果你想了解更多相關內容請查看下面相關鏈接

標簽: ASP.NET
相關文章:
主站蜘蛛池模板: 黄色91在线| 亚洲一区在线日韩在线深爱 | 一区二区三区av | 99久久精品免费看国产一区二区三区 | 亚洲精品一区二区三区在线看 | 黄色一级片黄色一级片 | 国产亚洲一区二区三区在线观看 | 国产一区二区三区免费播放 | 亚洲欧美日韩在线一区 | 在线视频a | 亚洲一区二区在线视频 | 精品一区二区在线观看 | 久久伊人成人网 | 亚洲激情一区二区 | 国产激情一区二区三区 | 久久草视频| 91久久国产 | www.99热.com| 久在线 | 国产精品久久久久久av公交车 | 国产精品亚洲欧美日韩一区在线 | 精品九九| 亚洲成人三级 | 久久成人精品 | 国产精品无码永久免费888 | 亚洲精品一区二区三区蜜桃久 | 日本久久久一区二区三区 | 在线国产一区二区 | 在线观看a视频 | 一色视频 | 九九热这里都是精品 | 国产小视频在线播放 | 午夜社区 | 亚洲一区二区三区在线视频 | 亚洲国产成人久久 | 亚洲一区中文字幕在线 | 国产一区二区三区91 | 色视频久久| 欧美一区二区三区免费 | 91免费观看国产 | 美女午夜视频 |