Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the end I ended up wrapping the following code in an HtmlHelper extension method. This would allow me to use something like Html.ActionLink(c => c.SomeAction(new MessageObject { Id = 1 } ))</p> <p>and have all the properties of the MessageObject created as RouteValues.</p> <pre><code> public static RouteValueDictionary GetRouteValuesFromExpression&lt;TController&gt;(Expression&lt;Action&lt;TController&gt;&gt; action) where TController : Controller { Guard.Against&lt;ArgumentNullException&gt;(action == null, @"Action passed to GetRouteValuesFromExpression cannot be null."); MethodCallExpression methodCall = action.Body as MethodCallExpression; Guard.Against&lt;InvalidOperationException&gt;(methodCall == null, @"Action passed to GetRouteValuesFromExpression must be method call"); string controllerName = typeof(TController).Name; Guard.Against&lt;InvalidOperationException&gt;(!controllerName.EndsWith("Controller"), @"Controller passed to GetRouteValuesFromExpression is incorrect"); RouteValueDictionary rvd = new RouteValueDictionary(); rvd.Add("Controller", controllerName.Substring(0, controllerName.Length - "Controller".Length)); rvd.Add("Action", methodCall.Method.Name); AddParameterValuesFromExpressionToDictionary(rvd, methodCall); return rvd; } /// &lt;summary&gt; /// Adds a route value for each parameter in the passed in expression. If the parameter is primitive it just uses its name and value /// if not, it creates a route value for each property on the object with the property's name and value. /// &lt;/summary&gt; /// &lt;param name="routeValues"&gt;&lt;/param&gt; /// &lt;param name="methodCall"&gt;&lt;/param&gt; private static void AddParameterValuesFromExpressionToDictionary(RouteValueDictionary routeValues, MethodCallExpression methodCall) { ParameterInfo[] parameters = methodCall.Method.GetParameters(); methodCall.Arguments.Each(argument =&gt; { int index = methodCall.Arguments.IndexOf(argument); ConstantExpression constExpression = argument as ConstantExpression; if (constExpression != null) { object value = constExpression.Value; routeValues.Add(parameters[index].Name, value); } else { object actualArgument = argument; MemberInitExpression expression = argument as MemberInitExpression; if (expression != null) { actualArgument = Expression.Lambda(argument).Compile().DynamicInvoke(); } // create a route value for each property on the object foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(actualArgument)) { object obj2 = descriptor.GetValue(actualArgument); routeValues.Add(descriptor.Name, obj2); } } }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload