如何将参数传递给 ASP.NET MVC 中的局部视图? [英] How to pass parameters to a partial view in ASP.NET MVC?

查看:37
本文介绍了如何将参数传递给 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 在 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});
}

这篇关于如何将参数传递给 ASP.NET MVC 中的局部视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆