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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:18:33  来源:igfitidea点击:

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

asp.net-mvcasp.net-mvc-3model-view-controllercontrollerasp.net-mvc-partialview

提问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.RenderPartialon 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.FirstNameand @Model.LastNameaccordingly.

你将能够访问@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);