Handle Null Model
This is a simple and neat way to handle model nullity, usually returned by OrDefault part of LINQ query. Also a smilar one can be used for an empty IEnumrable.
public class HandleEmptyModelAttribute
: ActionFilterAttribute {
public override void OnActionExecuted
(ActionExecutedContext filterContext) {
if(filterContext.Controller.ViewData.Model
== null) {
filterContext.Result =
new PartialViewResult {
ViewName = "Empty"
};
}
}
}
Written by alrz
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#.net
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#