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?
You need to create a view model. Something like this should do...
then from your action result pass the model
and you will be able to access
@Model.FirstName
and@Model.LastName
accordingly.Just:
make sure you add {} around Html.RenderPartial, as:
not
Following is working for me on dotnet 1.0.1:
./ourView.cshtml
./_ourPartial.cshtml
Use this overload (
RenderPartialExtensions.RenderPartial
on MSDN):so:
Here is another way to do it if you want to use ViewData:
And to retrieve the passed in values: