public void TheSuppliedFunctionShouldBeInvoked() { AntiForgeryHelperAdapter.ValidationFunction = () => _called = true; AntiForgeryHelperAdapter.Validate(); Assert.IsTrue(_called); }
public static ActionResult Update <T>(this IUpdatable <T> controller, T model, HttpVerbs httpVerb = HttpVerbs.Post) where T : class, INameable, IIdentifiable { bool redirect = false; switch (httpVerb) { case HttpVerbs.Get: break; case HttpVerbs.Head: break; case HttpVerbs.Options: break; default: AntiForgeryHelperAdapter.Validate(); redirect = true; break; } if (controller.ModelState.IsValid && redirect) { T updated; if (IsCreate(controller, model)) { updated = controller.Repository.Create(model); } else { var includes = CrudEntityExtensions.GetComplexPropertyNames(typeof(T).GetUpdateProperties()); updated = controller.Repository.Update(model, includes); } controller.Repository.SaveChanges(); return(new RedirectToRouteResult("Details", new RouteValueDictionary(new { action = "Details", id = updated.Id }))); } else { controller.ViewData.Model = controller.Builder.BuildUpdateView <T>(model); return(new ViewResult { ViewData = controller.ViewData, TempData = controller.TempData, ViewEngineCollection = controller.ViewEngineCollection }); } }
/// <summary> /// Validates the AntiForgery token and calls delete for the id on the Repository /// </summary> /// <typeparam name="T">A type that implements INameable and IIdentifiable</typeparam> /// <param name="controller">The controller being extended</param> /// <param name="id">The id of the object to be deleted</param> /// <returns>A redirect to the Index</returns> public static ActionResult Delete <T>(this IDeleteable <T> controller, T model) where T : class, INameable, IIdentifiable { AntiForgeryHelperAdapter.Validate(); controller.Repository.Delete(model); controller.Repository.SaveChanges(); return(new RedirectToRouteResult("Default", new RouteValueDictionary(new { action = "Index" }))); }
public static ActionResult Update <T>(this IUpdatable <T> controller, T model) where T : class, INameable, IIdentifiable { AntiForgeryHelperAdapter.Validate(); if (controller.ModelState.IsValid) { T updated; if (model.Id == 0) { updated = controller.Repository.Create(model); } else { var includes = CrudEntityExtensions.GetComplexPropertyNames(typeof(T).GetUpdateProperties()); updated = controller.Repository.Update(model, includes); } controller.Repository.SaveChanges(); return(new RedirectToRouteResult("Details", new RouteValueDictionary(new { action = "Details", id = updated.Id }))); } else { controller.ViewData.Model = controller.Builder.BuildUpdateView <T>(model); return(new ViewResult { ViewData = controller.ViewData, TempData = controller.TempData, ViewEngineCollection = controller.ViewEngineCollection }); } }