Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.RenderAction with Route Name

I am trying to utilize the Route attribute naming (ex: [Route(Name = "Abc123")]) in a controller action, and likewise be able to call that via Html.RenderAction, but it appears that this does not support route names. I assume this is because route names are reserved only for ones requested via HTTP and not called directly, but somewhat new to MVC so I'm at a loss.

I'm using MVC Attribute Routing entirely, and I do not have routes configured otherwise. It seems that I must define the route name, and the route name has to match the action method name. In doing so, however, I am getting naming conflicts when I try to name more than one Index.

I'm basically trying to support multiple partial views, each having their own controller, which serve as plugins/widgets on my site. So ideally each would have an action called Index.

Do you have a recommendation on how I can maintain the same naming? This allows me to call Html.RenderAction("Index", [ControllerName], [Model]) without the render name changing.

like image 325
trnelson Avatar asked Sep 16 '26 03:09

trnelson


1 Answers

You can use attribute routing with Html.RenderAction just you have to make sure that the action name in attribute is actual Name :

 [Route("Home/About")]
    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";

        return View();
    }

and in HTML you can have

@{Html.RenderAction("About", "home");}

It will work fine.

like image 58
pragya sharma Avatar answered Sep 17 '26 20:09

pragya sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!