簡體   English   中英

ASP.Net MVC如何使用Html.RenderAction將url參數傳遞給ChildAction

[英]ASP.Net MVC How to pass url parameters using Html.RenderAction to a ChildAction

我認為這將是直截了當的,但我設法把它搞砸了。 如果我想將URL參數傳遞給另一個操作,我是否必須為此創建新路由?

調節器

[ChildActionOnly]
    public ActionResult ContentSection(string sectionAlias, string mvcController, string mvcAction = null)

視圖

@Html.RenderAction("ContentSection", "Portal", new {sectionAlias = "TermsAndConditions", mvcController = "Portal", mvcAction = "ChoosePayment"})

錯誤

 CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments

這里的問題是

@Html.RenderAction("ContentSection", "Portal", new {sectionAlias = "TermsAndConditions", mvcController = "Portal", mvcAction = "ChoosePayment"})

相當於

<%= Html.RenderAction("ContentSection", "Portal", new {sectionAlias = "TermsAndConditions", mvcController = "Portal", mvcAction = "ChoosePayment"}) %>

ViewEngine (也是一個Response.Write )。 由於RenderAction返回void ,因此無法Response.Write它。 你想要做的是:

@{
     Html.RenderAction("ContentSection", "Portal", new {sectionAlias = "TermsAndConditions", mvcController = "Portal", mvcAction = "ChoosePayment"});
 }

@{ }語法表示Razor視圖引擎中的代碼塊,它等同於以下ViewEngine

<% Html.RenderAction("ContentSection", "Portal", new {sectionAlias = "TermsAndConditions", mvcController = "Portal", mvcAction = "ChoosePayment"}); %>

簡短的回答是:使用@ Html.Action()。

@Html.Action("ContentSection", "Portal", new {sectionAlias = "Terms", ...})

Nathan Anderson已經給出了很長的答案

這個答案的PS Credit真的歸於詹姆斯·奈爾(James Nail),后者在內森的回答中將其作為評論發表,但我發現它非常簡單和有價值,我認為應該是個人答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM