Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top