如何返回具有特定View的ActionResult(不是控制器名称)
我在MVC Controller中有一个方法SendMail。这个方法调用其他方法ValidateLogin。 这是validation登录的签名:
private ActionResult ValidateLogin(Models.ResetPassword model)
当我从SendMail调用ValidateLogin时,出现此exception是因为控制器尝试搜索视图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");