How to pass parameters to a partial view in ASP.NET MVC?

asp.net Mvcasp.net Mvc-3Model View-ControllerControllerasp.net Mvc-Partialview

asp.net Mvc Problem Overview


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?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

Here is another way to do it if you want to use ViewData:

@Html.Partial("~/PathToYourView.cshtml", null, new ViewDataDictionary { { "VariableName", "some value" } })

And to retrieve the passed in values:

@{
    string valuePassedIn = this.ViewData.ContainsKey("VariableName") ? this.ViewData["VariableName"].ToString() : string.Empty;
}

Solution 2 - asp.net Mvc

Use this overload (RenderPartialExtensions.RenderPartial on MSDN):

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

so:

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

Solution 3 - asp.net Mvc

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 accordingly.

Solution 4 - asp.net Mvc

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});

Solution 5 - asp.net Mvc

Following is working for me on dotnet 1.0.1:

./ourView.cshtml

@Html.Partial(
  "_ourPartial.cshtml",
  new ViewDataDictionary(this.Vi‌​ewData) {
    {
      "hi", "hello" 
    } 
  }
);

./_ourPartial.cshtml

<h1>@this.ViewData["hi"]</h1>

Solution 6 - asp.net Mvc

Just:

@Html.Partial("PartialName", Model);

Solution 7 - asp.net Mvc

@{
            Html.RenderPartial("_partialViewName", null, new ViewDataDictionary { { "Key", "Value" } });
        } 

in the place you want to show your partial,

 @{
    string valuePassedIn = this.ViewData.ContainsKey("Key") ? this.ViewData["Key"].ToString() : string.Empty;
}

in the partialview rendered,

To use the valuePassedIn --> @valuePassedIn

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSaeed NeamatiView Question on Stackoverflow
Solution 1 - asp.net MvcGarry EnglishView Answer on Stackoverflow
Solution 2 - asp.net MvcDavid WickView Answer on Stackoverflow
Solution 3 - asp.net MvcsimonlchildsView Answer on Stackoverflow
Solution 4 - asp.net MvcBlackTigerXView Answer on Stackoverflow
Solution 5 - asp.net MvclakesareView Answer on Stackoverflow
Solution 6 - asp.net MvcCássio Batista PereiraView Answer on Stackoverflow
Solution 7 - asp.net MvcV_G_View Answer on Stackoverflow