[英]How to return ActionResult with specific View (not the controller name)
我在MVC控制器中有一個方法SendMail。此方法調用其他方法ValidateLogin。 這是驗證登錄的簽名:
private ActionResult ValidateLogin(Models.ResetPassword model)
當我從SendMail調用ValidateLogin時,會出現此異常,因為控制器試圖搜索視圖SendMail,但我想加載ResetPassword視圖:
Global Error - The view 'SendMail' or its master was not found or no view engine supports the searched locations. The following locations were searched: ...
這是SendMail的代碼:
public ActionResult SendMail(string login)
{
return ValidateLogin(login);
}
如何覆蓋return語句上的View?
提前致謝
private ActionResult SendMail(string login)
{
return View("~/Views/SpecificView.cshtml")
}
您可以通過顯式指向特定位置來直接指向特定視圖。
View
方法有一個重載,可以將一個字符串傳遞給viewName
。 有時,您希望將string
作為模型傳遞,而asp.net框架會在嘗試使用值string
查找視圖時混淆它。 嘗試這樣的事情:
public ActionResult SendMail(string login)
{
this.Model = login; // set the model
return View("ValidateLogin"); // reponse the ValidateLogin view
}
最后,這就是解決方案
return View("ResetPassword", new ResetPassword
{
fields= fields
});
您可以按這樣的名稱返回視圖
return View("viewnamehere");
如果SendMail是POST,則應使用POST-REDIRECT-GET模式
public ActionResult SendMail(string login)
{
...
return RedirectToAction("ResetPassword", login);
}
public ActionResult ResetPassword(string login)
{
...
return View("ResetPassword", login);
}
這將保護您免受IE中的雙重攻擊
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.