I have an action that when I was working on during development was called by an @Html.Render action, rendered the partial view and worked just fine.
That partial view was rendering a table which inside had another @Html.RenderAction for each TR.
Like this:
<div id="fooDiv">
@{Html.RenderAction("ProjectData", "Functional", new { categoryId = Model.SelectedCategoryId});}
</div>
Which renders several other Partials one of which is:
<div id="FuncTableWrapper">
@{Html.RenderAction("FuncTable", Model.Id);}
</div>
Which renders:
<table id="funcTable">
<tr>
<th></th>
@foreach (var item in Model.Categories)
{
<th class="thFuncLangName">@item.Name</th>
}
</tr>
@foreach (var funcFeature in Model.FuncFeatures)
{
<tr>
<td class="tdfuncFeatureName" colspan="@(Model.Categories.Count() + 1)">@funcFeature.Name</td>
</tr>
<tr class="trAddFuncContent hideThis">
<td colspan="@(Model.Languages.Count() + 1)">
@{Html.RenderAction("AddFuncContent", new { funcFeatureId = funcFeature.Id });}
</td>
</tr>
</table>
This works fine. However, the render should not be done on page load but only when I click on a tab, I decided to call that action from jquery and populate the div I needed at that point.
$("#FuncData").click(function () {
$.ajax({
type: "POST",
url: '@Url.Action("ProjectData", "Functional")',
data: { "CategoryId": @(Model.SelectedCategoryId) },
dataType: "html",
success: function(result) {
populateDiv($("#fooDiv"), data);
}
});
});
When I do that I know get an error on the RenderAction calling the “AddFuncContent” action saying that “Child actions are not allowed to perform redirect actions.”
What difference is there between the page calling Html.RenderAction and javascript calling my action?
Using the POST method may be the issue – on the initial page load you are probably in a get request, and POST is often set up to save something then redirect.
Another difference is that only the ajax version actually uses a Url. RenderAction can look for the named action method directly, so doesn’t need to parse a Url. That means that it doesn’t matter if there is not a valid route for the action, or the route could also point to a different action.