Created
May 31, 2011 23:29
-
-
Save nelsonlaquet/1001488 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%ul.site-navigation | |
%li != Html.RouteLink("Dashboard").Dashboard | |
%li != Html.RouteLink("Projects").ListProjects | |
%li != Html.RouteLink("Account").Account | |
%li != Html.RouteLink("Logout").Logout | |
... | |
-foreach (var project in Model.Projects) | |
%tr | |
%td.name != Html.RouteLink(project.Details.Name).ShowProject(id: project.Id) | |
%td.actions | |
!= Html.RouteLink("edit").EditProject(id: project.Id) | |
!= Html.RouteLink("delete").DeleteProject(id: project.Id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost] | |
public ActionResult Edit(Guid id, ProjectsEdit form) | |
{ | |
PrepareViewModel(form, id); | |
if (form.Project == null) | |
return HttpNotFound(); | |
if (ModelState.IsValid) | |
{ | |
_projectsService.UpdateProjectDetails(id, form.Details); | |
return Redirect(Url.RouteUrl().ShowProject(id: id)); | |
} | |
return View(form); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DynamicRouteHelper : DynamicObject | |
{ | |
private readonly Func<string, RouteValueDictionary, object> _routeFactory; | |
/// <param name="routeFactory"> | |
/// A factory delegate that, given a route name and it's route values, will return an object that will | |
/// serve as the result of invoking a property or calling a method on this object (such as a string in | |
/// the case of Url. or as an HtmlString in the case of Html.) | |
/// </param> | |
public DynamicRouteHelper(Func<string, RouteValueDictionary, object> routeFactory) | |
{ | |
_routeFactory = routeFactory; | |
} | |
public override bool TryGetMember(GetMemberBinder binder, out object result) | |
{ | |
result = _routeFactory(binder.Name, new RouteValueDictionary()); | |
return true; | |
} | |
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) | |
{ | |
var argumentDictionary = args.Zip(binder.CallInfo.ArgumentNames, Tuple.Create).ToDictionary(k => k.Item2, v => v.Item1); | |
result = _routeFactory(binder.Name, new RouteValueDictionary(argumentDictionary)); | |
return true; | |
} | |
} | |
public static class UrlHelpers | |
{ | |
public static dynamic RouteUrl(this UrlHelper that) | |
{ | |
return new DynamicRouteHelper(that.RouteUrl); | |
} | |
} | |
public static class LinkHelpers | |
{ | |
public static dynamic RouteLink(this HtmlHelper that, string linkText) | |
{ | |
return new DynamicRouteHelper((name, values) => that.RouteLink(linkText, name, values)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment