2011-08-25 67 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