Skip to content

Instantly share code, notes, and snippets.

@corycook
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save corycook/58b2f841e970d6194835 to your computer and use it in GitHub Desktop.

Select an option

Save corycook/58b2f841e970d6194835 to your computer and use it in GitHub Desktop.
C# Web MVC BeginActionLink
using System.Collections.Generic;
using System.Web.Mvc;
namespace TestWeb.Extensions
{
public static class HtmlHelperExtensions
{
static Dictionary<string, object> ToDictionary(object o)
{
return o.GetType().GetProperties().ToDictionary(n => n.Name, n => n.GetValue(o, null));
}
public static MvcLink BeginActionLink(this HtmlHelper helper, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
var uh = new UrlHelper(helper.ViewContext.RequestContext);
var attr = ToDictionary(htmlAttributes);
attr.Add("href", uh.Action(actionName, controllerName, routeValues));
return new MvcLink(helper.ViewContext.Writer, attr);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment