'How to invoke an action in different controller from another controller
I've got a new ASP.NET MVC app I wrote using .NET 4.6.2. It's a simple app, just 1 page. However, a user had a problem today, so I started by trying to the values of the Message from the Exception object to use the Err.cshtml file in the Shared folder and Error controller. However, I've not used that before and a page I found to help with this doesn't take parameters passed it it. https://www.c-sharpcorner.com/UploadFile/618722/custom-error-page-in-Asp-Net-mvc/#:~:text=Custom%20Error%20Page%20in%20ASP.NET%20MVC%201%20First,an%20%5BHandleError%5D%20attribute%20to%20the%20Targeted%20Action%20Method.
So, then I thought I'd try creating another Error.cshtml file in the Error folder under Views, hoping it would still use the Error controller. However, that resulted in several other errors, depending upon how I specified the call to the RedirectToAction() method. If I specify the call like this: return RedirectToAction("Index", "Error", errText);, then it gives me a complains by giving me a HTTP 404 error. I really thought that would work, because the action name in the controller is Index. So, I tried using return RedirectToAction("Error", "Error", errText);, but that gives me a slightly different HTTP 404 errror.
I've searched for answers to this, but I either only find results of calling from one view to a controller or using ASP.NET Core. (I wish I could, but I cannot.) To complicate my problems, I cannot get to some of the results from my search because the results are on GitHub. And my employer is blocking GitHub.
Here's the code in my HomeController, which I'm having difficulties with:
public ActionResult Index(SuggestedText suggestedText)
{
try
{
SendAnonymously.SuggestedTextToEmail(suggestedText.SuggestionToBeEmailed);
}
catch (Exception ex)
{
var errText = new ErrorText();
errText.TextToDisplay = SendAnonymously.ReturnExceptions(ex);
//return View("../Shared/Error", errText);
return RedirectToAction("Index", "Error", errText);
}
return View(suggestedText);
}
The ErrorText class is very simple:
public class ErrorText
{
public string TextToDisplay { get; set; }
}
Solution 1:[1]
You can use TempData in your case when you are doing a redirect to your Error view:
public ActionResult Index(SuggestedText suggestedText)
{
try
{
SendAnonymously.SuggestedTextToEmail(suggestedText.SuggestionToBeEmailed);
}
catch (Exception ex)
{
var errText = new ErrorText();
errText.TextToDisplay = SendAnonymously.ReturnExceptions(ex);
//return View("../Shared/Error", errText);
TempData["ErrorText"] = errText;
//Redirect to Error view under Error Controller
return RedirectToAction("Error", "Error");
}
return View(suggestedText);
}
And then you can retrieve this value in Error method like this:
public ActionResult Error()
{
ErrorText errText=new ErrorText();
if(TempData["ErrorText"] !=null)
{
errText=TempData["ErrorText"] as ErrorText
}
return View(errText)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rahul Sharma |
