Memphis
Memphis

Reputation: 271

MVC RedirectToAction from one controller to another - View is not displayed issue

I have a controller in which is do some checks.

If for one reason or another and exception occurs I want to display the error messages in another view in another controller.

This is how my exception handler looks

  catch (Exception ex)
        {
            string infoMsg = "Delete the user and recreate is with an appropriate username of both a first and last name which is required for a doctor";

            string errorMsg = ex.Message;

          //  ErrorController ec = new ErrorController();

            return this.RedirectToAction("Index", "Error", new { errorMessage = errorMsg, infoMessage = infoMsg });

        }

This is ActionResult the receives the call.

   public ActionResult Index(string errorMessage, string infoMessage)
    {
        var db = new DB();

        OccuredError occuredError = new OccuredError();

        occuredError.ErrorMsg = errorMessage;
        occuredError.InfoMsg = infoMessage;

        DateTime dateTime = DateTime.Now;

        occuredError.TimeOfError = dateTime;

        db.OccuredErrors.InsertOnSubmit(occuredError);
        db.SubmitChanges();


        return View(occuredError);
    }

This is the error Index view which is strongly typed

<h2>Index</h2>

<table>
    <tr>
        <th></th>
        <th>
            Error Message
        </th>
        <th>
            Info Message
        </th>
        <th>
            Time Of Error
        </th>
    </tr>

<% foreach (var item in Model) { %>

    <tr>
        <td>
            <%= Html.ActionLink("Details", "Details", new { id=item.ErrorId })%> |
        </td>
        <td>
            <%= Html.Encode(item.ErrorId) %>
        </td>
        <td>
            <%= Html.Encode(item.ErrorMsg) %>
        </td>
        <td>
            <%= Html.Encode(item.InfoMsg) %>
        </td>
        <td>
            <%= Html.Encode(String.Format("{0:g}", item.TimeOfError)) %>
        </td>
    </tr>

<% } %>

</table>

My problem is that the User is NOT redirected from the initial view (The one that had the exception) to the Error index view. When debugging I can see that it runs through the Error Index ActionResult fine and the data is put in the DB. There is just something with the displaying of the Index view that cause the problem I think.

Any suggestions to what I am doing wrong here.

Upvotes: 1

Views: 2907

Answers (1)

Matt Griffiths
Matt Griffiths

Reputation: 1142

As a quick test, change: return View(occuredError); to: return View("Error", occuredError);

where "Error" is the name of the view.

Matt

Upvotes: 1

Related Questions