Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8662655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:47:09+00:00 2026-06-12T16:47:09+00:00

I am trying to change @{Html.RenderAction(Foo, TheAction);} to @{Html.RenderAction((FooController c) => c.TheAction );} which

  • 0

I am trying to change

@{Html.RenderAction("Foo", "TheAction");}

to

@{Html.RenderAction((FooController c) => c.TheAction );}

which is nicer in a lot of ways, it improves:

  • F12-navigation
  • Type usage detection (resharper etc)
  • Method usage detection
  • Strictness
  • …the possibility to keep dead links away from the project

…so I have written an extension class like this:

using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace V3NET.MVC.Extensions
{
    public static class HtmlHelperExtension
    {
        public static void RenderAction<T>
            (this HtmlHelper helper, Expression<Func<T, Func<int, int?, ActionResult>>> action)
        {
            var actionName =
                (
                    (MethodInfo)
                    (
                        (ConstantExpression)
                        (
                            (MethodCallExpression)
                            (
                                ((UnaryExpression)action.Body).Operand
                            )
                        ).Object
                    ).Value
               ).Name;

            var controllerType = action.Parameters[0].Type;
            var controllerName = new Regex("Controller$").Replace(controllerType.Name, "");

            helper.RenderAction(actionName, controllerName);
        }
    }
}

…but as you can see, I have to write it specific for an action taking int, int? as arguments.

How do I express this more generally so I dont have to write a zillion of overloads?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:47:10+00:00Added an answer on June 12, 2026 at 4:47 pm

    You could use Expression<Action<TController>>.

    Here’s an improved version of the helper you are trying to write which is taking into account many more scenarios that your helper doesn’t support such as asynchronous controller actions for example. It also adds a generic constraint for the type of the argument to be a Controller:

    public static class HtmlHelperExtension
    {
        public static void RenderAction<TController>(
            this HtmlHelper helper, 
            Expression<Action<TController>> action,
            object routeValues
        ) where TController: Controller
        {
            var body = action.Body as MethodCallExpression;
            if (body == null)
            {
                throw new ArgumentException("Must be a method call", "action");
            }
    
            var controllerName = typeof(TController).Name;
            if (!controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException("Target must end in Controller", "action");
            }
            controllerName = controllerName.Substring(0, controllerName.Length - "Controller".Length);
            if (controllerName.Length == 0)
            {
                throw new ArgumentException("Cannot route to controller", "action");
            }
    
            var actionName = GetTargetActionName(body.Method);
            helper.RenderAction(actionName, controllerName, routeValues);
        }
    
        private static string GetTargetActionName(MethodInfo methodInfo)
        {
            string name = methodInfo.Name;
            if (methodInfo.IsDefined(typeof(NonActionAttribute), true))
            {
                throw new InvalidOperationException(
                    string.Format(
                        CultureInfo.CurrentCulture, 
                        "Cannot call non action {0}", 
                        name
                    )
                );
            }
            var attribute = methodInfo.GetCustomAttributes(typeof(ActionNameAttribute), true).OfType<ActionNameAttribute>().FirstOrDefault<ActionNameAttribute>();
            if (attribute != null)
            {
                return attribute.Name;
            }
            if (methodInfo.DeclaringType.IsSubclassOf(typeof(AsyncController)))
            {
                if (name.EndsWith("Async", StringComparison.OrdinalIgnoreCase))
                {
                    return name.Substring(0, name.Length - "Async".Length);
                }
                if (name.EndsWith("Completed", StringComparison.OrdinalIgnoreCase))
                {
                    throw new InvalidOperationException(
                        string.Format(
                            CultureInfo.CurrentCulture, 
                            "Cannot call {0}Completed method", 
                            name
                        )
                    );
                }
            }
            return name;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to change the checked attribute of dynamically created html radio button
I am trying to change a tag of a html element. HTML <div id='content'>
I'm trying to change strings like this: <a href='../Example/case23.html'><img src='Blablabla.jpg' To this: <a href='../Example/case23.html'><img
I am trying to change HTML code within an IFrame (it's on the same
I am trying to change the html.erb files from my application, but when i
I am working with JQuery and I'm trying to change the html of a
hey, i'm trying to change the html of a div after an ajax request,
I'm trying to change my index.html to show a modal window if the referer
I'm trying to use ajax on a select box and change html elements with
I'm trying to change text-selection on my HTML page. I want to change the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.