如何在 ASP.NET MVC 中将参数传递给局部视图?

发布于 11-18 08:16 字数 385 浏览 4 评论 0原文

假设我有这个部分视图:

Your name is <strong>@firstName @lastName</strong>

可以通过仅子操作访问,例如:

[ChildActionOnly]
public ActionResult FullName(string firstName, string lastName)
{

}

并且我想在另一个视图中使用此部分视图:

@Html.RenderPartial("FullName")

换句话说,我希望能够将firstName和lastName从视图传递到部分视图。我该怎么做呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

青萝楚歌2024-11-25 08:16:12

如果您想使用 ViewData,还有另一种方法:

@Html.Partial("~/PathToYourView.cshtml", null, new ViewDataDictionary { { "VariableName", "some value" } })

并检索传入的值:

@{
    string valuePassedIn = this.ViewData.ContainsKey("VariableName") ? this.ViewData["VariableName"].ToString() : string.Empty;
}

Here is another way to do it if you want to use 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;
}
海的爱人是光2024-11-25 08:16:12

使用此重载(MSDN 上的RenderPartialExtensions.RenderPartial):

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

所以:

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

Use this overload (RenderPartialExtensions.RenderPartial on MSDN):

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

so:

@{Html.RenderPartial(
    "FullName",
    new { firstName = model.FirstName, lastName = model.LastName});
}
爱已欠费2024-11-25 08:16:12

您需要创建一个视图模型。像这样的事情应该做...

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;
     }

}

然后从您的操作结果传递模型

return View("FullName", new FullNameViewModel("John", "Doe"));

,您将能够相应地访问 @Model.FirstName@Model.LastName

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.

放飞的风筝2024-11-25 08:16:12

确保在 Html.RenderPartial 周围添加 {},如下所示:

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

不是

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

make sure you add {} around Html.RenderPartial, as:

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

not

@Html.RenderPartial("FullName", new { firstName = model.FirstName, lastName = model.LastName});
婴鹅2024-11-25 08:16:12

以下是我在 dotnet 1.0.1 上的工作:

./ourView.cshtml

@Html.Partial(
  "_ourPartial.cshtml",
  new ViewDataDictionary(this.Vi‌​ewData) {
    {
      "hi", "hello" 
    } 
  }
);

./_ourPartial.cshtml

<h1>@this.ViewData["hi"]</h1>

Following is working for me on dotnet 1.0.1:

./ourView.cshtml

@Html.Partial(
  "_ourPartial.cshtml",
  new ViewDataDictionary(this.Vi‌​ewData) {
    {
      "hi", "hello" 
    } 
  }
);

./_ourPartial.cshtml

<h1>@this.ViewData["hi"]</h1>
药祭#氼2024-11-25 08:16:12

只是:

@Html.Partial("PartialName", Model);

Just:

@Html.Partial("PartialName", Model);
↘人皮目录ツ2024-11-25 08:16:12
@{
            Html.RenderPartial("_partialViewName", null, new ViewDataDictionary { { "Key", "Value" } });
        } 

在您想要显示部分的地方,

 @{
    string valuePassedIn = this.ViewData.ContainsKey("Key") ? this.ViewData["Key"].ToString() : string.Empty;
}

在渲染的部分视图中,

使用 valuePassedIn --> @valuePassedIn

@{
            Html.RenderPartial("_partialViewName", null, new ViewDataDictionary { { "Key", "Value" } });
        } 

in the place you want to show your partial,

 @{
    string valuePassedIn = this.ViewData.ContainsKey("Key") ? this.ViewData["Key"].ToString() : string.Empty;
}

in the partialview rendered,

To use the valuePassedIn --> @valuePassedIn

德意的啸2024-11-25 08:16:12

我刚刚遇到这个问题,我也有类似的情况,

我的问题如下:
我有多个变量需要传递给我创建的部分视图 我

创建的解决方案

@{
await Html.RenderPartialAsync("YourPartialViewName",new { NumberOfProducts = ViewData["NumberOfProducts"], UserName = ViewData["UserName"] });
}

在上面的代码中,我创建了一个匿名对象并将其发送到视图
下面的代码是解释如何检索发送的数据
通过这个对象

     <span>
          @Model.UserName
      </span>

     <span>
          @Model.NumberOfProducts
      </span>

希望这有帮助

I just came across this question, and I have a similar situation

my problem is as follows :
I have more than one variable I need to pass to the partial view I created

The solution I have created

@{
await Html.RenderPartialAsync("YourPartialViewName",new { NumberOfProducts = ViewData["NumberOfProducts"], UserName = ViewData["UserName"] });
}

In the above code, I created an anonymous object and sent it to the view
and the following code is to explain how to retrieve the data that sent
through this object

     <span>
          @Model.UserName
      </span>

     <span>
          @Model.NumberOfProducts
      </span>

hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文