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

查看:364
本文介绍了如何将参数传递到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?

推荐答案

使用这个过载(<一href=\"http://msdn.microsoft.com/en-us/library/dd492962.aspx\"><$c$c>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天全站免登陆