Cannot access ViewBag in partial view in ASP.NET MVC3

I have a controller that calls a view. The view has a PartialView called be @Html.Partial("ViewName", model). This works great.

But in the controller I want to put something in a bag, which would be difficult to put in the viewmodel, I move on to the view. There is no problem accessing the ViewBag in the PartialView ViewBag , but it does not return anything in PartialView .

Is it possible to use the ViewBag in this case, or should I “crack” this data into the model that I pass to the view (and the model goes to PartialView , and the model goes to PartialView embedded in the first PartialView )?

+49
asp.net-mvc-3 viewbag
Apr 20 2018-11-11T00:
source share
7 answers

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

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

In the index view, I add a partial view:

 @Html.Partial("ViewName") 

And in a partial view, I rendered the message:

 @ViewBag.Message 

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

 @ViewContext.Controller.ViewBag.Message 
+43
Apr 20 2018-11-21T00:
source share

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

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

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

Remove new ViewDataDictionary(ViewBag) , so you should write

 @Html.Partial("PartialViewName", Model) 
+30
Jun 17 '13 at 11:56 on
source share

TehOne's comment says:

I know this is a bit outdated, but for future reference, I solved it using ViewContext.Controller.ViewBag.Property. Of course, this means that the ViewBag property that you are trying to access has been set to the controller, but I think this is a fairly common case.

This is what worked for me.

+9
May 01 '15 at 18:12
source share

I have had the same issue lately, but the answers here have been workarounds as they suggest not using the Html.Partial overload with 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 (for example, I need it to change the HtmlFieldPrefix ), and because in the view you need to know what partial view you need in the ViewBag , so it should not be a problem to name everything parameters from the ViewBag to be copied to the new ViewBag used in the partial view (apparently, the ViewBag uses the values ​​from viewData ).

+7
May 22 '15 at 22:29
source share

My senario was, I had a partial view with Model IEnumerable<SampleModel> However, I had to go through using, coz Model.SampleModelList may be null

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

The simple solution has been since PartialView is now also part of the view. Each element in the view can access the PartialView, so I set the ViewBag data for the data element in the html view instead of the ViewBag.

 <div class="showcase-heading" id="dvValue" data-id="@Model.ContentId">@Model.Name</div> 
0
Aug 01 '15 at 19:30
source share

I had the same problem with a very simple 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 links to the ViewBag to extract some dynamic values ​​passed from the controller. It turned out that the name of the partial presentation matters. Deleting a partial page and re-creating it using Add -> View -> MVC 5 View, but the page naming of _AppIntro.cshtml is fixed.

0
Oct 25 '17 at 15:28
source share

> In general, when you use Html.Partial;

 Html.Partial("partialViewName"); 

The model that is dispatched for parentView can be used for in componentViewName. In addition, the ViewData that is dispatched for parentView can also be used for a partial ViewName.

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

 Html.Partial("partialViewName", newModel); 

You cannot get to the model that was submitted for parentView. Therefore, from today on, the Model that is active in partalViewName is newModel. The ViewData that is dispatched for parentView can also be used for a partial ViewName.

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

The model that is submitted for parentView can also be used for partial viewing.

I AM.

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

Here, the ViewData that was sent for parentView is overwritten with the 'new ViewDataDictionary'.

Here, if there is a ViewBag for parentView, you will not be able to achieve this if you write code like the one above.

II.

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

This usage is the same as the first (s).

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 block of code, you can access the ViewData and ViewBag that are sent for parentView in partalViewName.

0
Apr 12 '19 at 11:37
source share



All Articles