MVC RedirectToAction 从一个控制器到另一个控制器 - 视图不显示问题

发布于 2024-09-19 08:33:47 字数 2181 浏览 5 评论 0原文

我有一个控制器,可以在其中进行一些检查。

如果由于某种原因发生异常,我想在另一个控制器的另一个视图中显示错误消息。

这就是我的异常处理程序的样子

  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 });

        }

这是接收调用的 ActionResult。

   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);
    }

这是强类型的错误索引视图

<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>

我的问题是用户没有从初始视图(出现异常的视图)重定向到错误索引视图。调试时,我可以看到它很好地运行了错误索引 ActionResult 并且数据已放入数据库中。我认为索引视图的显示有些问题导致了问题。

对我在这里做错的事情有任何建议。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最初的梦 2024-09-26 08:33:47

作为快速测试,更改:
返回视图(ocusedError);
到:
return View("Error",occurredError);

其中“Error”是视图的名称。

马特

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

where "Error" is the name of the view.

Matt

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文