Search code examples
asp.net-mvcasp.net-mvc-3model-view-controllercontrollerasp.net-mvc-partialview

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


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?


Solution

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