繁体   English   中英

如何使用特定的View(不是控制器名称)返回ActionResult

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM