无法在 ASP.NET MVC3 中的部分视图中访问 ViewBag

发布于 2024-11-02 11:18:42 字数 384 浏览 3 评论 0原文

我有一个调用视图的控制器。在视图中有一个名为 @Html.Partial("ViewName", model) 的 PartialView 这工作正常。

但是在控制器中,我希望将一些东西放入视图袋中,而这些东西很难放入我传递给视图的视图模型中。 主视图访问 ViewBag 没有问题,但在 PartialView 中它不返回任何内容。

在这种情况下是否可以使用 ViewBag,或者我应该将这些数据“破解”到传递给视图的模型中(以及传递给 PartialView 的模型),以及我传递给嵌套在第一个 PartialView 中的 PartialView 的模型)?

I have a controller calling a view. In the view there is a PartialView called be @Html.Partial("ViewName", model). This works fine.

But in the controller I wish to put something in the viewbag what would be hard to put in the viewmodel I pass to the view.
The main view have no problem accessing the ViewBag, but in the PartialView it does not return anything.

Is it possible to use the ViewBag in this case or should I "hack" this data into the model I pass to the view (and the model I pass to the PartialView, and the model I pass to the PartialView nested in the first PartialView)?

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

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

发布评论

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

评论(8

ヤ经典坏疍 2024-11-09 11:18:42

这应该没有任何问题。在我的 HomeController Index 操作中,我向 ViewBag 添加一条消息:

ViewBag.Message = "Welcome to ASP.NET MVC!";

在索引视图上,我添加部分视图:

@Html.Partial("ViewName")

在部分视图上,我渲染消息:

@ViewBag.Message

从下面的评论中:当您将模型传递给局部视图。然后你可以参考原来的ViewBag

@ViewContext.Controller.ViewBag.Message

That should work without any problems. In my HomeController Index action I add a message to the ViewBag:

ViewBag.Message = "Welcome to ASP.NET MVC!";

On the Index View I add the partial view:

@Html.Partial("ViewName")

And on the partial view I render the message:

@ViewBag.Message

From the comments below: there seems to be a problem when you pass a model to the partial view. Then you can refer to the original ViewBag with

@ViewContext.Controller.ViewBag.Message
等你爱我 2024-11-09 11:18:42

如果您使用 Html.Partial() 的重载,其中 viewData 是输入参数之一,例如:

@Html.Partial("PartialViewName", Model, new ViewDataDictionary(ViewBag))

那么您的部分视图将看不到原始数据查看包。

删除 new ViewDataDictionary(ViewBag),所以你应该写

@Html.Partial("PartialViewName", Model)

If you are using an overload of the Html.Partial() where viewData is one of the input parameters, for example:

@Html.Partial("PartialViewName", Model, new ViewDataDictionary(ViewBag))

then your partial view will not see data from your original ViewBag.

Remove new ViewDataDictionary(ViewBag), so you should write

@Html.Partial("PartialViewName", Model)
意犹 2024-11-09 11:18:42

在评论 TehOne 中说:

我知道这有点旧了,但为了将来的参考,我解决了这个问题
使用 ViewContext.Controller.ViewBag.Property。当然这意味着
您尝试访问的 ViewBag 属性已在
控制器,但我认为这是一个很常见的情况。

这对我有用。

In a comment TehOne said:

I know this is a bit old, but for future reference, I solved this by
using ViewContext.Controller.ViewBag.Property. Of course this means
that the ViewBag property you are trying to access was set in the
controller, but I think that is a common enough case.

Which is what worked for me.

旧人哭 2024-11-09 11:18:42

最近我遇到了同样的问题,但这里的答案只是解决方法,因为他们建议不要使用 viewData 使用 Html.Partial 的重载。但是,如果您必须使用这些重载,我相信正确的答案是:

@Html.Partial(
     "PartialViewName",
     model_object,
     new ViewDataDictionary() { { "Value1InViewBag", ViewBag.Value1InViewBag }, { "Value2InViewBag", ViewBag.Value2InViewBag } }
)

我认为如果您需要传递不同的 ViewDataDictionary (例如,我用它来更改 HtmlFieldPrefix ),则应该使用这个答案>) 并且因为在视图中您应该知道 ViewBag 中需要哪些部分视图,因此将 ViewBag 中的所有参数命名为复制到新的 ViewBag 中应该不会有问题代码> 用于部分视图(显然 ViewBag 使用来自 ViewData 的值)。

Recently I was experiencing same problem, but the answers here were just workarounds as they propose not to use the overload of Html.Partial using viewData. But if you had to use these overloads I believe the correct answer is:

@Html.Partial(
     "PartialViewName",
     model_object,
     new ViewDataDictionary() { { "Value1InViewBag", ViewBag.Value1InViewBag }, { "Value2InViewBag", ViewBag.Value2InViewBag } }
)

I think this answer should be used if you need to pass different ViewDataDictionary (e.g. I us it for changing HtmlFieldPrefix) and because in the view you should know, what partial view wil need in ViewBag, so it should not be problem to name all the parameters from ViewBag to be copied into new ViewBag used in partial view (apparently ViewBag uses values from ViewData).

半夏半凉 2024-11-09 11:18:42

>一般场景下,当你使用Html.Partial时;

Html.Partial("partialViewName");

为parentView发送的模型可以在partialViewName中使用。此外,为parentView发送的ViewData也可以用于partialViewName。

> 作为一种特殊情况,当您使用 Html.Partial 并且要发送 Model..

Html.Partial("partialViewName", newModel);

您无法访问为parentView发送的 Model。
因此,从现在开始,partialViewName 中活动的 Model 就是 newModel。
为parentView发送的viewData也可用于partialViewName。

> 作为一种特殊情况,当您使用 Html.Partial 并且要发送 ViewDataDictionary..

为parentView发送的模型也可以用于partialViewName

I。

@Html.Partial("partialViewName", new ViewDataDictionary { { "key", value }, { "key2", value2 }  })

这里,发送给parentView的ViewData被“new ViewDataDictionary”覆盖。

这里,如果有一个针对parentView的ViewBag,如​​果你像上面那样编写代码,你就无法到达它。

II.

ViewDataDictionary viewDataDictionary =  new ViewDataDictionary();
viewDataDictionary.Add("key", value);
viewDataDictionary.Add("key2", value2);
@Html.Partial("partialViewName", viewDataDictionary)

此用法与第一个 (I.) 相同。

III.

ViewDataDictionary viewDataDictionary = ViewData; //If you use this code block,  ViewBag which is sent for parent View is not lost.
viewDataDictionary.Add("key", value);
viewDataDictionary.Add("key2", value2);
@Html.Partial("partialViewName", viewDataDictionary)

通过此代码块,您可以到达在partialViewName中为parentView发送的ViewData和ViewBag

> In a general scenario, when you use Html.Partial;

Html.Partial("partialViewName");

The Model that is sent for parentView, can be used for in the partialViewName. Moreover, the ViewData which is send for parentView can also be used for partialViewName.

> As a special case, when you use Html.Partial and if you want to send Model..

Html.Partial("partialViewName", newModel);

You cannot reach the Model which was sent for parentView.
Therefore, from now on the Model which is active in the partialViewName is the newModel.
The viewData which is send for parentView can be used also for partialViewName.

> As a special case, when you use Html.Partial and if you want to send ViewDataDictionary..

The Model which is send for parentView can be used also for partialViewName

I.

@Html.Partial("partialViewName", new ViewDataDictionary { { "key", value }, { "key2", value2 }  })

Here, the ViewData which was sent for parentView overwrite by 'new ViewDataDictionary'.

Here, If there is a ViewBag which is for parentView, you cannot reach that if you write the code like above one.

II.

ViewDataDictionary viewDataDictionary =  new ViewDataDictionary();
viewDataDictionary.Add("key", value);
viewDataDictionary.Add("key2", value2);
@Html.Partial("partialViewName", viewDataDictionary)

This usage is same as first one (I.).

III.

ViewDataDictionary viewDataDictionary = ViewData; //If you use this code block,  ViewBag which is sent for parent View is not lost.
viewDataDictionary.Add("key", value);
viewDataDictionary.Add("key2", value2);
@Html.Partial("partialViewName", viewDataDictionary)

With this code block, You can reach the ViewData and ViewBag which are sent for parentView in the partialViewName

陌伤ぢ 2024-11-09 11:18:42

我在一个非常基本的页面上遇到了同样的问题:

@Html.Partial("_AppIntro")

<div class="form-group row">
    <div class="col-md-10">
        <a href="/Ag/Application/1" class="btn btn-default btn-primary">Next</a>
    </div>
</div>

并且部分视图仅包含一些对 ViewBag 的引用的文本,以获取从控制器传递的一些动态值。事实证明,部分视图的名称很重要。删除部分页面并使用“添加”->“重新创建”查看-> MVC 5 View 但命名页面 _AppIntro.cshtml 修复了它。

I was having this same problem with a very basic page:

@Html.Partial("_AppIntro")

<div class="form-group row">
    <div class="col-md-10">
        <a href="/Ag/Application/1" class="btn btn-default btn-primary">Next</a>
    </div>
</div>

And the partial view contained only text with some references to ViewBag to fetch some dynamic values passed from the controller. It turned out that the name of the partial view mattered. Removing the partial page and recreating it with Add -> View -> MVC 5 View but naming the page _AppIntro.cshtml fixed it.

子栖 2024-11-09 11:18:42

我的情况是,我对模型 IEnumerable有了部分视图
但是我必须通过使用,因为 Model.SampleModelList 可以为 null

@Html.Partial("PartialViewName", Model.SampleModelList, new ViewDataDictionary())

简单的解决方案是,因为 PartialView 现在也是 View 的一部分,View 上的每个元素都可以通过 PartialView 访问,所以我确实将 ViewBag 数据设置为View html 上的数据元素,而不是 ViewBag。

<div class="showcase-heading" id="dvValue" data-id="@Model.ContentId">@Model.Name</div>

My senario was, I had partial view with Model IEnumerable<SampleModel>
However I had to pass using, coz Model.SampleModelList can be null

@Html.Partial("PartialViewName", Model.SampleModelList, new ViewDataDictionary())

The simple solution was since PartialView is also now part of View every element on View can be accessed by PartialView so I did set the ViewBag data to data-element on View html, instead of ViewBag.

<div class="showcase-heading" id="dvValue" data-id="@Model.ContentId">@Model.Name</div>
清欢 2024-11-09 11:18:42

就我而言,@ViewContext.Controller.ViewBag.Message 效果不佳。
我有一个部分视图,它是从具有不同模型的不同控制器引用的,并继承自基本模型类。在这种情况下,Controller.ViewBag 为 null。

我的解决方案是:

@{
var dummy = ViewContext.Controller.ViewData.TryGetValue("CommonRules", out object rules);
}
...
@Html.Raw(rules)

In my case @ViewContext.Controller.ViewBag.Message didn't work as well.
I have a partial view that is referenced from different controllers with different models, inherited from a base model class. In this case the Controller.ViewBag comes as null.

My solution is:

@{
var dummy = ViewContext.Controller.ViewData.TryGetValue("CommonRules", out object rules);
}
...
@Html.Raw(rules)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文