如何将参数传递到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")

在换句话说,我希望能够从视图局部视图传递的firstName答lastName的。我应该怎么办呢?

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

使用这个过载(RenderPartialExtensions.RenderPartial$c$c> MSDN上):

Use this overload (RenderPartialExtensions.RenderPartial on MSDN):

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

这样:

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