0

I am trying to return the parent View when I call an ActionResult from within a Partial View. If I know the parent view I can just type in return View("Index");, but I can't because their could be multiple parent views as this partial view is shared... So how do I return the correct parent View in the ActionResult?

This seems so simple, but it has me stumped...

Update. Here is the Partial View:

@model Website.Models.PostModel

@using (Html.BeginForm("SubmitPost", "Home", FormMethod.Post))
{
@Html.TextAreaFor(m => m.Message, 2, 50, null)
<br /><br />
<div class="ui-widget">
    Notes: @Html.TextBox("Notes", "", new { @class = "ui-autocomplete-input" })
</div>
<br />
<p>
    <input type="submit" value="Post" />
</p>

}

Allensb
  • 260
  • 3
  • 13

1 Answers1

4

I assume you're using @{Html.RenderAction("Action");} in your view to call

[ChildActionOnly]
public ActionResult Action()
{
    var model = context.Data.Where(x => x);

    return PartialView("PartialView", model);
}

if so, then you could specify your routeValues as well. In your view you would call

@{Html.RenderAction("Action", new {viewName = "parentView"});}

and in you controller:

[ChildActionOnly]
public ActionResult Action(string viewName)
{
    var model = context.Data.Where(x => x);

    if (model == null) return View(viewName);

    return PartialView("PartialView", model);
}
Major Byte
  • 4,101
  • 3
  • 26
  • 33
  • I am using HTML.BeginForm for my submit button. Do I need to pass the parent's model to the partial view? – Allensb Aug 25 '11 at 22:53
  • Finally figured it out. Had to pass the view name to the partial render like you pointed out. Thanks for the help! – Allensb Aug 26 '11 at 03:57