当操作类型为:RedirectToRouteResult 时在视图中显示错误

发布于 2024-11-27 22:40:45 字数 1519 浏览 2 评论 0原文

下面的代码没有在视图中显示我的错误,如何确保我的错误显示在视图中?

//注意,如果某处出现问题,我会抛出规则异常,如果是这样,我会将错误复制到模型状态

    [HttpPost]
    public RedirectToRouteResult TaskDueDate(int id, int taskid)
    {

        var duedate = Request.Form["duedate"];
        var duetime = Request.Form["duetime"];

        try
        {
            var newduedate = DateHelper.GoodDate(duedate, duetime);
            _service.SetTaskDueDate(id, taskid, newduedate);

            this.FlashInfo("success, task due date has been updated...");
        }
        catch (RulesException ex)
        {
            ex.CopyTo(ModelState);
        }

        return RedirectToAction("TaskDetail");
    }

ex.CopyTo 扩展方法:

public static void CopyTo(this RulesException ex, ModelStateDictionary modelstate)
    {
        CopyTo(ex,modelstate,null);
    }

    public static void CopyTo(this RulesException ex, ModelStateDictionary modelstate, string prefix)
    {
        prefix = string.IsNullOrEmpty(prefix) ? "" : prefix + ".";
        foreach (var propertyerror in ex.Errors)
        {
             string key = ExpressionHelper.GetExpressionText(propertyerror.Property);
            modelstate.AddModelError(prefix + key, propertyerror.Message);
        }
    }

在我看来,我基本上有:

        <div id="Errors">
            <span id="ServerResponse"></span>
            <%= Html.ValidationSummary(false, "")%>
        </div>

我认为模型状态已清除,并且重定向上没有错误???

The code below is not showing my errors in the view, how do I ensure my errors display in the view?

//Note I throw a rules exception if something goes wrong somewhere, if so I copy the errors onto the modelstate

    [HttpPost]
    public RedirectToRouteResult TaskDueDate(int id, int taskid)
    {

        var duedate = Request.Form["duedate"];
        var duetime = Request.Form["duetime"];

        try
        {
            var newduedate = DateHelper.GoodDate(duedate, duetime);
            _service.SetTaskDueDate(id, taskid, newduedate);

            this.FlashInfo("success, task due date has been updated...");
        }
        catch (RulesException ex)
        {
            ex.CopyTo(ModelState);
        }

        return RedirectToAction("TaskDetail");
    }

ex.CopyTo extension method:

public static void CopyTo(this RulesException ex, ModelStateDictionary modelstate)
    {
        CopyTo(ex,modelstate,null);
    }

    public static void CopyTo(this RulesException ex, ModelStateDictionary modelstate, string prefix)
    {
        prefix = string.IsNullOrEmpty(prefix) ? "" : prefix + ".";
        foreach (var propertyerror in ex.Errors)
        {
             string key = ExpressionHelper.GetExpressionText(propertyerror.Property);
            modelstate.AddModelError(prefix + key, propertyerror.Message);
        }
    }

In my view I basically have:

        <div id="Errors">
            <span id="ServerResponse"></span>
            <%= Html.ValidationSummary(false, "")%>
        </div>

I think the model state gets cleared and no errors remain on a redirect???

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

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

发布评论

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

评论(1

绅士风度i 2024-12-04 22:40:45

是的,重定向是对浏览器的响应,告诉浏览器发出新请求,因此您拥有的任何上下文都会丢失。

当有任何内容要显示时,您可以显示错误页面:

if (ModelState.Count > 0) {
  return View("ErrorPage");
} else {
  return RedirectToAction("TaskDetail");
}

您需要将操作方法​​的返回类型更改为基类 AcionResult 以返回不同类型的操作结果。

Yes, a redirect is a response to the browser that tells it to make a new request, so any context that you have is lost.

You could show an error page when there is anything to display on it:

if (ModelState.Count > 0) {
  return View("ErrorPage");
} else {
  return RedirectToAction("TaskDetail");
}

You would need to change the return type of your action method to the base class AcionResult to return different kinds of action results.

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