tags:

views:

2342

answers:

2
<body>
<div id="header">
<div class="title">SPORTS STORE</div>
</div>
<div id="categories">
<% Html.RenderAction("Menu", "Nav"); %>
</div>
<div id="content">
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</div>
</body>

This is a sample code from Steven Sandersons' book "Pro ASP.NET MVC Framework."

This code wokrs with mvc v1 and mvcContLib. What it does, it renders "Menu()" view of "Nav : Controller". Since ASP.NET MVC v2 includes the Partial() and RenderPartial() functionality, i tried to implement it, but this code doesnt work. I tried to modify it in a sever ways, but it seems there's no overload function that renders patrial view, taking as parameters: 1) Controller name 2) View name

I'm sure i'm not the first who is implementing RenderAction() in this way, so, there must be a workaround. Please help.

Thanks, Ilya.

A: 

RenderAction is in MVC2 (docs here). It sounds like you've changed your code to use RenderPartial instead which is completely different. Change it back to use RenderAction and you should be ok. If you don't have it in your version, perhaps you need to update to the latest beta?

tvanfosson
Hi tvanfosson, thanks for your reply.I tried RenderAction earlier, it throws an exception like "no sutable route found in rotes table". It doesnt seem reasonable to add any additional rotes, beacuse i'm not acually using route: Controller and View are invoked Direclty, right?Thanks, Ilya.
portland
There is a signature for RenderAction that takes the action and controller, in that order. Your code looks correct -- do you have the latest MVC2 beta? Is it possible that you have a conflict with the MVCContrib lib -- you should remove it from your project, I would think.
tvanfosson
I have no references to MVCContlib. I'm also sure to have the latest vertion of MVC, which is included in MS VS 2010 RC Ultimate. Additional information will be provided in few hours. Thanks for such a quick replies, tvanfosson.
portland
I've checked: <% Html.RenderAction("Menu", "Nav" as string); %>throws an exception No route in the route table matches the supplied values.VS 2010 RC, no mvcContlib used.
portland
+1  A: 

http://stackoverflow.com/questions/2408961/how-to-render-partial-view-in-asp-net-mvc-2-using-controller-and-action

I found the problem. I always remove {controller}/{action} route and customize all my routes with lowercase REST like URLs. But for Html.RenderAction it is necessary to have such general route. I added that general route to the end of my routes list and it worked. – Mahdi Feb 22 at 14:42

Although i still don't understand, how EXACTLY this works, why is suck route nessesary, and what are route's constraints. Maybe i will do some research later.

portland
It is because RenderAction is much closer to what happens when a controller action is initially mapped via routing. I do agree it would be nice if one could not expose the route but still have RenderAction work. In the meantime, you can expose only that specific route, put [ChildActionOnly] on the actions and you'll not have the default routing in place. As you hinted at, with route constraints, you can lessen the impact the presence of the route may have on your available "routing namespace" (not an issue in most cases but an important consideration with sites that are partially CMS).
Cymen