ASP.NET?MVC實(shí)現(xiàn)登錄后跳轉(zhuǎn)到原界面
有這樣的一個(gè)需求:提交表單,如果用戶沒有登錄,就跳轉(zhuǎn)到登錄頁,登錄后,跳轉(zhuǎn)到原先表單提交這個(gè)頁面,而且需要保持提交表單界面的數(shù)據(jù)。
提交表單的頁面是一個(gè)強(qiáng)類型視圖頁,如果不考慮需要保持提交表單界面的數(shù)據(jù),可以先設(shè)計(jì)這樣的一個(gè)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); }
可是,雖然回到了表單提交的強(qiáng)類型視圖頁,表單數(shù)據(jù)卻沒有得以保持。
于是,想到了使用如下方式:
return View("someview", somemodel);
someview的名稱如何獲取呢?
public ActionResult Index() { return View(new Student()); }
以上,如果我們獲取到action的名稱就相當(dāng)于獲取到視圖的名稱!
重新設(shè)計(jì)Model:
public class Student { public string Name { get; set; } public string ControllerName { get; set; } public string ActionName { get; set; } }
可以先從路由中把a(bǔ)ction名稱拿到,然后賦值給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 = "我又回來了~~"; //如果是登錄,先驗(yàn)證,驗(yàn)證成功執(zhí)行下面的代碼 return View(student.ActionName, student); } }
以上,student.ActionName值既是action名稱也是view名稱。
在提交表單的強(qiáng)類型視圖頁:
@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="提交"/> }
所以,面對本篇開始描述的需求,僅僅跳轉(zhuǎn)是不夠的,需要向某個(gè)視圖傳遞Model,而其中的關(guān)鍵是:
1、從路由中獲取action名稱
2、action名稱和view名稱一致
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
- Asp.Net MVC記住用戶登錄信息下次直接登錄功能
- Asp.net mvc驗(yàn)證用戶登錄之Forms實(shí)現(xiàn)詳解
- ASP.NET MVC SSO單點(diǎn)登錄設(shè)計(jì)與實(shí)現(xiàn)代碼
- Asp.net mvc 權(quán)限過濾和單點(diǎn)登錄(禁止重復(fù)登錄)
- [Asp.Net MVC4]驗(yàn)證用戶登錄實(shí)現(xiàn)實(shí)例
- ASP.NET MVC5網(wǎng)站開發(fā)之登錄、驗(yàn)證和注銷管理員篇1(六)
- ASP.NET MVC結(jié)合JavaScript登錄、校驗(yàn)和加密
- ASP.NET?MVC5網(wǎng)站開發(fā)用戶登錄、注銷(五)
相關(guān)文章
ASP.NET(C#)中操作SQLite數(shù)據(jù)庫實(shí)例
最近項(xiàng)目中有使用到SQLite數(shù)據(jù)庫,于是查找資料,編寫了一個(gè)ASP.NET基于C#語言的SQLite數(shù)據(jù)庫操作實(shí)例.大家看代碼就可以看懂了,和以往使用ADO.NET操作SQL數(shù)據(jù)庫類似.2009-12-12ASP.NET實(shí)現(xiàn)多域名多網(wǎng)站共享Session值的方法
實(shí)現(xiàn)功能:可設(shè)置哪些站點(diǎn)可以共享Session值,這樣就防止別人利用這個(gè)去訪問,要想實(shí)現(xiàn)這個(gè)功能就必須得把Session值 放入數(shù)據(jù)庫中, 所有我們先在VS命令工具下注冊一個(gè)2011-11-11asp.net System.Guid ToString五種格式
這篇文章主要介紹了asp.net System.Guid ToString五種格式,需要的朋友可以參考下2017-02-02Asp.Net Oracle數(shù)據(jù)的通用操作類
Asp.Net連接Oracle數(shù)據(jù)的通用操作類并且利用它對數(shù)據(jù)庫查詢、匯總、更新等操作演示2009-12-12Linkbutton控件在項(xiàng)目中的簡單應(yīng)用
Button控件可分為button控件、LinkButton控件、ImageButton控件三類,而LinkButton控件則在頁面上顯示為一個(gè)超級(jí)鏈接,下面與大家分享下其具體應(yīng)用2013-05-05