当操作类型为:RedirectToRouteResult 时在视图中显示错误
下面的代码没有在视图中显示我的错误,如何确保我的错误显示在视图中?
//注意,如果某处出现问题,我会抛出规则异常,如果是这样,我会将错误复制到模型状态
[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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,重定向是对浏览器的响应,告诉浏览器发出新请求,因此您拥有的任何上下文都会丢失。
当有任何内容要显示时,您可以显示错误页面:
您需要将操作方法的返回类型更改为基类
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:
You would need to change the return type of your action method to the base class
AcionResult
to return different kinds of action results.