Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Suppose that I have this partial view:

Your name is <strong>@firstName @lastName</strong>

which is accessible through a child only action like:

[ChildActionOnly]
public ActionResult FullName(string firstName, string lastName)
{

}

And I want to use this partial view inside another view with:

@Html.RenderPartial("FullName")

In other words, I want to be able to pass firstName ans lastName from view to partial view. How should I do that?

share|improve this question
Very closely related question: stackoverflow.com/questions/1909134/… – Chris Moschini Mar 18 at 23:17

3 Answers

up vote 16 down vote accepted

use this overload (docs):

public static void RenderPartial( this HtmlHelper htmlHelper, string partialViewName, Object model )

so:

@{Html.RenderPartial("FullName", new { firstName = model.FirstName, lastName = model.LastName});}

share|improve this answer
1  
Well, tried that, but I'm getting The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult‌​)' has some invalid arguments error. How should I implement the controller method? Should it have input parameters? – Saeed Neamati Jul 1 '11 at 15:06
The RenderPartial method returns void, as the views render directly into the output stream, not into a string. I edited my answer to fix this issue. – David Wick Jul 1 '11 at 16:07
did you add the {} around the call? it needs to be @{Html.RenderPartial("FullName", new { firstName = model.FirstName, lastName = model.LastName});} not @Html.RenderPartial("FullName", new { firstName = model.FirstName, lastName = model.LastName}); – BlackTigerX Jul 1 '11 at 16:18
@blacktigerx, yup. – David Wick Jul 1 '11 at 16:44
2  
You also don't need the brackets. @Html.Partial() is the same as @{Html.RenderPartial();} but much prettier. They have the same overloads as well. – smdrager Jun 21 '12 at 18:03
show 3 more comments

You need to create a view model. Something like this should do...

public class FullNameViewModel()
{
     public string FirstName { get; set; }
     public string LastName { get; set; }

     public FullNameViewModel() { } 

     public FullNameViewModel(string firstName, string lastName)
     {
          this.FirstName = firstName;
          this.LastName = lastName;
     }

}

then from your action result pass the model

return View("FullName", new FullNameViewModel("John", "Doe");

and you will be able to access @Model.FirstName and @Model.LastName

share|improve this answer

make sure you add {} around Html.RenderPartial, as:

@{Html.RenderPartial("FullName", new { firstName = model.FirstName, lastName = model.LastName});}

not

@Html.RenderPartial("FullName", new { firstName = model.FirstName, lastName = model.LastName});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.