C# asp.net MVC RC1 RenderPartial ViewDataDictionary

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/495351/
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-08-04 05:37:54  来源:igfitidea点击:

asp.net MVC RC1 RenderPartial ViewDataDictionary

c#.netasp.net-mvc

提问by Mark79

I'm trying to pass a ViewDataobject from a master page to a view user control using the ViewDataDictionary.

我正在尝试ViewData使用ViewDataDictionary.

The problem is the ViewDataDictionaryis not returning any values in the view user control whichever way I try it.

问题是ViewDataDictionary无论我尝试哪种方式,都没有在视图用户控件中返回任何值。

The sample code below is using an anonymous object just for demonstration although neither this method or passing a ViewDataobject works.

下面的示例代码使用匿名对象只是为了演示,尽管此方法或传递ViewData对象都不起作用。

Following is the RenderPartialhelper method I'm trying to use:

以下是RenderPartial我尝试使用的辅助方法:

<% Html.RenderPartial("/Views/Project/Projects.ascx", ViewData.Eval("Projects"), new ViewDataDictionary(new { Test = "Mark" })); %>

and in my view user control i do the following:

在我的视图用户控件中,我执行以下操作:

<%= Html.Encode(ViewData["Test"]) %>

Why does this not return anything?

为什么这不返回任何东西?

Thanks for your help.

谢谢你的帮助。

EDIT:

编辑:

I'm able to pass and access the strongly typed model without any problems. it's the ViewDataDictionarywhich I'm trying to use to pass say just a single value outside of the model...

我能够毫无问题地传递和访问强类型模型。这是ViewDataDictionary我试图用来传递的,只是模型之外的一个值......

采纳答案by Nick Berardi

Have you tried:

你有没有尝试过:

<% Html.RenderPartial("~/Views/Project/Projects.ascx", ViewData); %>

Also have you verified ViewData["Test"] is in the ViewData before you are passing it? Also note that when passing your ViewData to a Partial Control that it is important to keep the Model the same.

在传递之前,您是否还验证了 ViewData["Test"] 是否在 ViewData 中?另请注意,将 ViewData 传递给 Partial Control 时,保持模型相同很重要。

Nick

缺口

回答by Tim Scott

In your main view:

在您的主要观点中:

<% Html.RenderPartial("~/Views/Project/Projects.ascx", ViewData["Projects"]); %>

Either in you controller or your main view:

在您的控制器或主视图中:

ViewData["Test"] = "Mark";

If you don't specify a model or view data dictionary in RenderPartail, it uses the ones from the containing view.

如果您没有在 RenderPartail 中指定模型或视图数据字典,它将使用包含视图中的数据字典。

回答by Tim Scott

I'm using the RTM version of ASP.Net MVC, but with:

我正在使用 ASP.Net MVC 的 RTM 版本,但具有:

<% Html.RenderPartial("~/Views/Project/Projects.ascx", new ViewDataDictionary(new {Key = "Some value"})); %>

Try the following when referencing the value in your partial view:

在局部视图中引用值时,请尝试以下操作:

<%= ViewData.Eval("Key") %>

That seems to have done the trick for me. It will also work if you just do this:

这似乎对我有用。如果您只是这样做,它也将起作用:

<% Html.RenderPartial("~/Views/Project/Projects.ascx", new {Key = "Some value"}); %>

Hope this helps.

希望这可以帮助。

回答by Josiah Ruddell

Don't know if anyone still cares but I used a KeyValuePair for the ViewDataDictionary.

不知道是否还有人关心,但我为 ViewDataDictionary 使用了 KeyValuePair。

 <% Html.RenderPartial("ItemRow", item, new ViewDataDictionary{
        new KeyValuePair<string, object>("url", url),
        new KeyValuePair<string, object>("count", count),
        new KeyValuePair<string, object>("className", className)
 }); %>

This way you can write everything in one statement. The KVPs can be accessed in the view by:

通过这种方式,您可以在一个语句中编写所有内容。可以通过以下方式在视图中访问 KVP:

<%= ViewData["url"] %>
<%= ViewData["count"] %>
<%= ViewData["className"] %>

While what I passed through the model can be accessed through Model.*

而我通过模型传递的内容可以通过模型访问。*

回答by Alan

This is the neatest way I've seen to do this:

这是我见过的最简洁的方法:

<% Html.RenderPartial("/Views/Project/Projects.ascx", Model, new ViewDataDictionary{{"key","value"}});%>

It may be a little hackish, but it let's you send the model through AND some extra data.

这可能有点骇人听闻,但它可以让您通过和一些额外的数据发送模型。