views:

237

answers:

1

I have a Microsoft MVC project with an action "Foo" whose view ("Foo.aspx") contains the lines:

<%= Html.ActionLink("mylinktext1", "bar") %>
<%= Html.ActionLink<MyController>(x => x.Bar(), "mylinktext2") %>

When I hit this from a web browser or load it from an AJAX call, it properly returns:

<a href="/bar">mylinktext1</a>
<a href="/Bar">mylinktext2</a>

But when I call the action from another view like this:

<% Html.RenderAction<MyController>(x => x.Foo()); %>

Then the links are rendered without targets.

<a href="">mylinktext1</a>
<a href="">mylinktext2</a>

Why would this be happening, and how do I work around it?

+1  A: 

I don't know if that is what you are doing wrong, but I have never used Html.RenderAction with actions that return ASPX views. When I call RenderAction, I make sure that I am calling a controller action that returns ASCX View User Control.

Typically .ASPX file is an entire page and you can't (shouldn't) render this inside another page. So I think you should make it View User Control (ASCX) and put it either in Shared or in controller's view folder.

Based on your comment: Of course this is fine. You just return your data as model to your views/view user controls. When you load them thru AJAX, you should consider implementing Render View to String. Search the Google or Stack for more information on it. You can also search for a thing called JsonPox attribute for your action methods - also implemented somewhere on the internet. It will enable decorating your action methods so that they are able to return HTML view, XML or JSON if that's what you also might need.

mare
Does it make sense to have AJAX actions that return user control views?My project has actions which should be directly accessible by HTTP, but I also want access to render their views from other pages. For example, say I have an AJAX call to dynamically update part of a page, but when that page is first rendered, I want to incorporate that part of the page without calling the action, because the page action has already computed all the values the AJAX action would have to compute. The page can just fill the variables into the partial. Is this a sensible thing to do?
Brian Kendig
I switched my .aspx file to a .ascx, and the same problem is still happening. <%= Html.ActionLink("mylinktext1", "bar") %> displays a link with href="".
Brian Kendig
Aha. Using <% Html.RenderAction<MyController>(x => x.Foo()); %> causes the problem. Using <% Html.RenderAction("Foo", "MyController"); %> works fine. Why would the typed version fail and the magic-strings version work?
Brian Kendig