[フレーム]
Last Updated: February 25, 2016
·
718
· alrz

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

AltStyle によって変換されたページ (->オリジナル) /