asp.net-mvc 如何将参数传递给 ASP.NET MVC 中的局部视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6549541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass parameters to a partial view in ASP.NET MVC?
提问by Saeed Neamati
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?
换句话说,我希望能够将 firstName 和 lastName 从视图传递到局部视图。我该怎么做?
采纳答案by David Wick
Use this overload (RenderPartialExtensions.RenderPartial
on MSDN):
使用此重载(RenderPartialExtensions.RenderPartial
在 MSDN 上):
public static void RenderPartial(
this HtmlHelper htmlHelper,
string partialViewName,
Object model
)
so:
所以:
@{Html.RenderPartial(
"FullName",
new { firstName = model.FirstName, lastName = model.LastName});
}
回答by Garry English
Here is another way to do it if you want to use ViewData:
如果您想使用 ViewData,这是另一种方法:
@Html.Partial("~/PathToYourView.cshtml", null, new ViewDataDictionary { { "VariableName", "some value" } })
And to retrieve the passed in values:
并检索传入的值:
@{
string valuePassedIn = this.ViewData.ContainsKey("VariableName") ? this.ViewData["VariableName"].ToString() : string.Empty;
}
回答by simonlchilds
You need to create a view model. Something like this should do...
您需要创建一个视图模型。这样的事情应该做...
public class FullNameViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public FullNameViewModel() { }
public FullNameViewModel(string firstName, string lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
}
}
then from your action result pass the model
然后从您的操作结果传递模型
return View("FullName", new FullNameViewModel("John", "Doe"));
and you will be able to access @Model.FirstName
and @Model.LastName
accordingly.
你将能够访问@Model.FirstName
和@Model.LastName
相应。
回答by BlackTigerX
make sure you add {} around Html.RenderPartial, as:
确保在 Html.RenderPartial 周围添加 {},如:
@{Html.RenderPartial("FullName", new { firstName = model.FirstName, lastName = model.LastName});}
not
不是
@Html.RenderPartial("FullName", new { firstName = model.FirstName, lastName = model.LastName});
回答by lakesare
Following is working for me on dotnet 1.0.1:
以下在dotnet 1.0.1上对我来说有效:
./ourView.cshtml
./ourView.cshtml
@Html.Partial(
"_ourPartial.cshtml",
new ViewDataDictionary(this.Vi??ewData) {
{
"hi", "hello"
}
}
);
./_ourPartial.cshtml
./_ourPartial.cshtml
<h1>@this.ViewData["hi"]</h1>
回答by Cássio Batista Pereira
Just:
只是:
@Html.Partial("PartialName", Model);