Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

asp.net-mvc

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 
like image 462
JBeckton Avatar asked Feb 10 '11 23:02

JBeckton


People also ask

What is the difference between HTML action and HTML RenderAction?

The difference between the two is that Html. RenderAction will render the result directly to the Response (which is more efficient if the action returns a large amount of HTML) whereas Html. Action returns a string with the result.

What is the use of RenderAction in MVC?

RenderAction(HtmlHelper, String, String, RouteValueDictionary) Invokes the specified child action method using the specified parameters and controller name and renders the result inline in the parent view.

What is render action?

A render action is a public method on the controller class. You can define a render action method to return any data, but you can only safely use it if it returns an HTML markup string.


1 Answers

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"}); %> 
like image 200
Nathan Anderson Avatar answered Sep 22 '22 16:09

Nathan Anderson