Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return ActionResult with specific View (not the controller name)

I have a method SendMail in the MVC Controller.This method calls other method ValidateLogin. This is the signature of the Validate Login:

private ActionResult ValidateLogin(Models.ResetPassword model) 

When I call the ValidateLogin from SendMail, this exception appears because the controller try to search a view SendMail, but I want to load the ResetPassword View:

Global Error - The view 'SendMail' or its master was not found or no view engine supports the searched locations. The following locations were searched: ... 

This is the code of the SendMail:

public ActionResult SendMail(string login) {         return ValidateLogin(login); } 

How Can I override the View on the return statement?

Thanks in advance

like image 690
clement Avatar asked Oct 14 '14 11:10

clement


People also ask

How do I return a view from a different controller?

Just add your View to the Shared subdirectory and you're good to go. If you do return View("~/Views/Wherever/SomeDir/MyView. aspx") You can return any View you'd like.

Can I return ActionResult Instead of view results?

When you set Action's return type ActionResult , you can return any subtype of it e.g Json,PartialView,View,RedirectToAction.

How do I return an ActionResult model?

Index(). ViewData. Model; If your method signature's return type is ActionResult instead of ViewResult, you will need to cast it to ViewResult first.

How do you return a partial view from controller?

In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.


2 Answers

private ActionResult SendMail(string login) {             return View("~/Views/SpecificView.cshtml") } 

You can directly point towards specifc view by pointing to their location explicitly ..

like image 101
Kaushik Vatsa Avatar answered Sep 27 '22 22:09

Kaushik Vatsa


finally, this was the solution

return View("ResetPassword", new ResetPassword             {                 fields= fields             }); 
like image 24
clement Avatar answered Sep 27 '22 23:09

clement