36

I thought this was going to be straight forward but I managed to hose it up some how. If I want to pass URL parameters to another action do I have to create a new route for that?

controller

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

view

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

error

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

2 Answers 2

46

The problem here is that

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

Is the equivalent to

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

In the the Webforms ViewEngine (which is also the same a Response.Write). Since RenderAction returns void, you cannot Response.Write it. What you want to do is this:

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

The @{ } syntax signifies a code block in the Razor view engine, which would be equivalent to the following the the Webforms ViewEngine:

<% Html.RenderAction("ContentSection", "Portal", new {sectionAlias = "TermsAndConditions", mvcController = "Portal", mvcAction = "ChoosePayment"}); %>
Sign up to request clarification or add additional context in comments.

3 Comments

Or you can call @Html.Action(...) instead, in order to call it as a method that returns your content (if you don't like using the curly brackets, etc.)
Thanks, I just figured it out by reading another similar post. Thanks! I think this got me once before too..
Yes, this is really a well-written answer. Thanks Nathan! And thanks to James Nail, too. Your hint was also valuable.
17

The short answer is to use @Html.Action() like this:

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

The long answer was already given by Nathan Anderson.

P.S. Credit for this answer really goes to James Nail, who posted it as a comment in Nathan's answer, but I found it so easy and valuable that I thought it should be an individual answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.