PDA

View Full Version : [CLOSED] ViewData and partial views



ingbabic
Jan 15, 2016, 10:48 AM
Is ViewData local to partial view? For example I have one TabPanel with two tabs (panels) which are partial views (for example ObjectView.cshtml) showing different objects. If I set for example to one tab ViewData["objectId"]="123" and to other tab ViewData["objectId"]="234" and clicking between those two will I have correct ViewData in each partial view? If not, what is the way to remedy this? If it is for example Hidden fields, how could in most easy way I get value from it, from javascript? I want to mention that I cant use IDs, because I might use same partial view in many tabs or windows. I need to take value from local hidden field (from that very partial view), but without using any IDs, or so. Or maybe there's better idea?

Daniil
Jan 15, 2016, 11:33 AM
Hi @ingbabic,

In my best understanding each Controller call sends a unique ViewData to a View.

So, if you render two same partial views with individual controller calls, then you have unique ViewDatas. Then if you set Hiddens' values from ViewData, they should be unique for each partial view.


but without using any IDs


ItemID and ComponentQuery is a nice approach then.
http://forums.ext.net/showthread.php?21393&p=92621&viewfull=1#post92621

ingbabic
Jan 15, 2016, 1:50 PM
Did I understand you correctly: You want to say that if I have same partial view which I instantiate with separate controller calls sending ViewData with same id but different value(for example ViewData["objectId"]), then all partial views opened will have same ViewData["objectId"]- the last one set. And in that case if I want to get ID I have to keep it in page (hidden field) which I access with ComponentQuery.

Is it right?

fabricio.murta
Jan 19, 2016, 9:38 PM
Well, from that two discussions:

- MVC3 - Passing data beyond the model to Partial view (http://stackoverflow.com/questions/7177153)

- Can't access ViewBag in a partial view in ASP.NET MVC3 (http://stackoverflow.com/questions/5729784)

I understand that a ViewData inherits its data from the parent. So two siblings will have all data from parent in common but, data set individually on them is not present in the parent nor the other sibling. I.e.:
parent defines: dataOne
sibling1 defines: dataTwo
sibling2 defines: dataThree.

Parent will just have 'dataOne'. Sibling1 will have 'dataOne, dataTwo'. Sibling2 will have 'dataOne, dataThree'.

This indicates that your wish of defining the same object name in two partial views will not result in conflict unless you explicitly sync them.

I hope this helps!

Daniil
Jan 20, 2016, 1:01 PM
Did I understand you correctly: You want to say that if I have same partial view which I instantiate with separate controller calls sending ViewData with same id but different value(for example ViewData["objectId"]), then all partial views opened will have same ViewData["objectId"]- the last one set.


I rather meant the opposite. I think that each View has its unique ViewData. But I am not sure that I understand you. Do you mean that ViewData exists after a View is already rendered? I always thought it doesn't exists and it just disappears when a View is rendered. A Controller sends a ViewData object to a View, then that View uses ViewData as needed, then View is being rendered and ViewData is not actual anymore. A developer needs to store ViewData in a View manually (e.g. in hidden fileds). So, it is a way how I always thought about ViewData:) But then I looked into the docs (https://msdn.microsoft.com/en-us/library/system.web.mvc.viewpage.viewdata%28v=vs.118%29.asp x) and there is a phrase in the Remarks section that really confused me:


The view can add to or change the data, which will be sent to the controller when the view is posted as part of a request.

Interesting how it could work and where ViewData is stored then. Is it automatically mapped to hidden fields to be submitted via a regular post? Then it is mapped to ViewData in a controller? Oh, I don't quite have any idea how it could work. I found a Stackoverflow question - ViewData empty after POST (http://stackoverflow.com/questions/2931805/asp-net-mvc-2-viewdata-empty-after-post) - there a few guys are saying that ViewData is not posted to a Controller. Yes, it is what I always thought. I posted a comment there asking how they understand that phrase in the docs.


And in that case if I want to get ID I have to keep it in page (hidden field) which I access with ComponentQuery.

With ItemID and ComponentQuery I just suggested a way to avoid using IDs, but still be able to access things.