繁体   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