2011-08-25 36 views
0

我想从部分视图中调用ActionResult时返回父视图。如果我知道父视图,我可以在返回视图(“索引”),但我不能,因为他们可以是多个父视图,因为这个部分视图是共享...所以我如何返回正确的父视图在ActionResult中?共享的部分视图ActionResult问题

这看起来很简单,但它让我难以置信......

更新。下面是部分查看:

@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> 

}

回答

4

我假设你在你的视图中使用@{Html.RenderAction("Action");}调用

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

    return PartialView("PartialView", model); 
} 

如果是这样,那么你可以指定你的routeValues为好。 在你看来,你会打电话

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

和你的控制器:

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

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

    return PartialView("PartialView", model); 
} 
+0

我使用HTML.BeginForm我的提交按钮。我是否需要将父母的模型传递给部分视图? – Allensb

+0

终于搞明白了。必须像您指出的那样将视图名称传递给部分渲染。谢谢您的帮助! – Allensb