返回一个视图与它的模型从ActionFilterAttribute [英] Returning a view with it's model from an ActionFilterAttribute
问题描述
在实现使用内置的验证,佣工在强类型的视图,您通常会创建控制器内的try / catch块,并返回一个视图与它的相应的模型作为参数传递给<$ C错误处理$ C>查看()方法:
控制器
When implementing error-handling using the built-in validation-helpers on a strongly-typed view, you usually create a try/catch block within the controller and return a view with it's corresponding model as a parameter to the View()
method:
The controller
public class MessageController : Controller
{
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Models.Entities.Message message)
{
try
{
// Insert model into database
var dc = new DataContext();
dc.Messages.InsertOnSubmit(message);
dc.SubmitChanges();
return RedirectToAction("List");
}
catch
{
/* If insert fails, return a view with it's corresponding model to
enable validation helpers */
return View(message);
}
}
}
视图
<%@ Page
Language="C#"
Inherits="System.Web.Mvc.ViewPage<Models.Entities.Message>" %>
<%= Html.ValidationSummary("Fill out fields marked with *") %>
<% using (Html.BeginForm()) { %>
<div><%= Html.TextBox("MessageText") %></div>
<div><%= Html.ValidationMessage("MessageText", "*") %></div>
<% } %>
我实现了一个简单的错误处理程序在ActionFilterAttribute的形式,这将能够要么重定向到一个一般性的错误观点,或重定向到其抛出了一个异常的看法,并让验证,佣工春天的生活。
I've implemented a simple error-handler in the form of an ActionFilterAttribute, which will be able to either redirect to a generic error view, or redirect to the view which threw an exception, and let the validation-helpers spring to life.
下面是我的ActionFilterAttribute的外观:
Here's how my ActionFilterAttribute looks:
public class ErrorLoggingAttribute : ActionFilterAttribute, IExceptionFilter
{
private Boolean _onErrorRedirectToGenericErrorView;
/// <param name="onErrorRedirectToGenericErrorView">
/// True: redirect to a generic error view.
/// False: redirect back the view which threw an exception
/// </param>
public ErrorLoggingAttribute(Boolean onErrorRedirectToGenericErrorView)
{
_onErrorRedirectToGenericErrorView = onErrorRedirectToGenericErrorView;
}
public void OnException(ExceptionContext ec)
{
if (_onErrorRedirectToGenericErrorView)
{
/* Redirect back to the view where the exception was thrown and
include it's model so the validation helpers will work */
}
else
{
// Redirect to a generic error view
ec.Result = new RedirectToRouteResult(new RouteValueDictionary
{
{"controller", "Error"},
{"action", "Index"}
});
ec.ExceptionHandled = true;
}
}
}
重定向到引发异常的看法是相当简单的。但这里的起脚:为了验证佣工工作,你需要提供的视图与它的模型
Redirecting to the view which threw the exception is fairly simple. But here's the kicker: In order for the validation helpers to work, you need to provide the view with it's model.
你会如何返回的抛出异常的视图,并提供与它的对应型号的看法? (在这种情况下 Models.Entities.Message
)。
How would you return the view which threw an exception and provide the view with it's corresponding model? (In this case Models.Entities.Message
).
推荐答案
我得到它的工作!
对于一些奇怪的原因,所有我需要做的是在的ViewData code>传递到一个新的
ResultView
。
For some odd reason, all I needed to do was to pass on the ViewData
to a new ResultView
.
下面是完整的code:
Here's the complete code:
public class ErrorLoggingAttribute : ActionFilterAttribute, IExceptionFilter
{
private String _controllerName, _actionName;
private Boolean _redirectToGenericView = false;
public ErrorLoggingAttribute()
{
}
public ErrorLoggingAttribute(String actionName, String controllerName)
{
_controllerName = controllerName;
_actionName = actionName;
_redirectToGenericView = true;
}
void IExceptionFilter.OnException(ExceptionContext ec)
{
// log error
if (_redirectToGenericView)
{
ec.Result = new RedirectToRouteResult(new RouteValueDictionary
{
{"controller", _controllerName},
{"action", _actionName}
});
}
else
{
ec.Result = new ViewResult
{
ViewName = ((RouteData) ec.RouteData).Values["action"].ToString(),
TempData = ec.Controller.TempData,
ViewData = ec.Controller.ViewData
};
}
ec.ExceptionHandled = true;
}
}
这里是你将如何使用该属性上的控制器动作,重定向到了同样的观点(与的它的相关模型),使标准的审定,助手踢,发生异常时:
Here's how you would use the attribute on a controller-action, to redirect to the same view (with it's associated model) to enable standard validation-helpers to kick in, when an exception occurs:
[ErrorLogging]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Models.Entities.Message message)
{
var dc = new Models.DataContext();
dc.Messages.InsertOnSubmit(message);
dc.SubmitChanges();
return RedirectToAction("List", new { id = message.MessageId });
}
和这里是你将如何使用的属性,重定向到一个通用的观点,当发生异常:
And here's how you would use the attribute, to redirect to a generic view, when an exception occurs:
[ErrorLogging("ControllerName", "ViewName")]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Models.Entities.Message message)
这是一个的完整的逻辑的分离。没有在控制器,但非常基础。
This is a complete separation of logic. Nothing in the controller but the very basics.
这篇关于返回一个视图与它的模型从ActionFilterAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!