Asp.net-Mvc
帶有錯誤消息的重定向到操作
我的 AdminUsers 視圖中的網格上有一個連結
grid.Column(header: "", format: (item) => (condition ? Html.ActionLink("Impersonate", "Impersonate", "Admin", new { id = item.username }, null) : Html.Label("Impersonate"), style: "webgrid-column-link"),在控制器中,我有
public ActionResult Impersonate(string id) { string result = ORCA.utilities.users.setImpersonation(id); if(result == "nocommonfields") return RedirectToAction("AdminUsers", "Admin"); else return RedirectToAction("terms_of_use", "Forms"); }當我返回 AdminUsers 頁面時,如何發送錯誤消息以顯示?
您可以使用TempData
if(result == "nocommonfields") { TempData["ErrorMessage"]="This is the message"; return RedirectToAction("AdminUsers", "Admin"); }在您的 AdminUsers 操作中,您可以閱讀它
public ActionResult AdminUsers() { var errMsg=TempData["ErrorMessage"] as string; //check errMsg value do whatever you want now as needed }請記住,TempData 的生命週期很短。會話是臨時數據背後的備份儲存。
或者,您也可以考慮在您的查詢字元串中發送一個標誌並在您的下一個操作方法中讀取它並決定要顯示什麼錯誤消息。