2013-11-23 24 views
0

我有模型(POCO實體)等StudentCourseStandard等我有相應的控制器如StudentController等我有其顯示在數據庫中的所有相應的實體的列表中的每個模型的圖Index。例如,StudentController.Index()返回/Student/Index視圖。但是,如果數據庫中沒有學生記錄,而不是返回Index視圖,則我將重定向至Navigation控制器的Empty操作方法,即NavigationController.Empty(),該操作將返回/Navigation/Empty視圖。這是爲所有模型實體類完成的。如何通過ASP.NET MVC中的超鏈接返回上一個視圖?

現在,在空白頁面上,我希望有一個超鏈接返回上一頁。所以我在NavigationController類中創建了一個名爲GoBack()的操作方法,其中我重定向到上一個視圖。但是,如何訪問關於此操作方法中上一頁的信息?還是有更好的方法來做到這一點?我不想使用後退按鈕。

+0

如果你正在構建一個APP-你可能想要調查SPA(單頁應用)體系結構http://en.wikipedia.org/wiki/Single-page_application – jfrankcarr

回答

1

就我而言,這裏有幾條路線。您可以使用會話或應用程序緩存來存儲訪問las的頁面,然後使用RedirectToActionGoBack()操作中獲取該頁面(例如通過存儲路由)。

但是,也許更好和無狀態的方法是通過使視圖模型具有用於最後使用的控制器&動作的兩個屬性來呈現超鏈接。然後,您可以通過調用/Navigation/Empty操作的行爲結果(當沒有任何記錄時)傳遞這些結果。

視圖模型

public class NavigationVM 
{ 
public string LastAction {get;set;} 
public string LastController {get;set;} 
} 

導航控制器動作

public ActionResult Empty(string lastAction, string lastController) 
{ 
var vm = new NavigationVM() 
{ 
LastAction = lastAction, 
LastController = lastController 
} 
return View(vm); 
} 

查看

@model = Namespace.NavigationVM 

@Html.ActionLink("LinkName", Model.LastAction, Model.LastController) 

編輯

如果你需要找出學生管理員被調用的地方(在你的例子中),你可以用同樣的方法去做。 I.e .:使用額外路線值呈現鏈接到StudentsController

StudentController:

public ActionResult Index(string lastAction, string lastController) 

{ 
    .... // no students 
return RedirectToAction("Empty", "Navigation", new RouteValueDictionary(new { lastAction = "Index", lastController= "Student"})); 

} 

查看超鏈接到學生控制器(使用分別呈現這個視圖lastActionlastController動作和控制器):

@Html.ActionLink("Get students", "Index", "Student", new { lastAction= "Index", lastController = "CallingController" }, null) 
+0

好的。這個解決方案將讓我使'回去'超鏈接動態。但是我仍然必須將控制器和操作傳遞給'/ Student/Index'操作方法,並傳遞給'/ Navigation/Empty'方法。我從哪裏得到這些信息? 換句話說,當調用'/ Student/Index'動作方法時,我需要知道它被調用的位置,以便我可以將這些信息傳遞給'/ Navigation/Empty'方法。 –

+0

查看編輯示例。 – bump