views:

54

answers:

2

Hi,

I need a good example or guideline which could give me a pattern for data passing between view and partial view.

For example I have a view which contains several partial views. I refresh these partial view by ajax. So I need to pass data among view and its partial views.

The best way would be if I could pass data without using Temp data dictionary.

Anybody know a good article about this?

l.

+2  A: 

1) One way would be to call RenderAction() and as such go to controller action and then controller would return the data to the View or PartialView as a Model for it. In general, you should be using strongly typed views and partial views wherever possible and try to avoid using ViewData and TempData.

2) You can also use RenderPartial("some view", ObjectData) to send an entire business object or POCO object or CLR variable to the partial view.

3) You can also have partial views strongly typed the same as your main view (the one that would then load up those partials). If you have the same type then doing just RenderPartial("some view") would render your partial view with the same Model that the "main" or "parent" view has.

I don't think I have more than 3-5 usages of ViewData or TempData in my apps, I almost never use it.

mare
This is good advice, +1
Tejs
Thanks your answer.. I try to follow your recomended steps.But I have a strange bug. I pass same model to partial views and "parent" view. I set the a different value to viemodel property but after the ajax refreshing I see the original value on the page not the new that I setted via ajax request in controller action.Do you have any idea why happen this?
post the code of your view and your controller action and let us see.
mare
A: 

Thanks your answer.. I try to follow your recomended steps. But I have a strange bug. I pass same model to partial views and "parent" view. I set the a different value to viemodel property but after the ajax refreshing I see the original value on the page not the new that I setted via ajax request in controller action. Do you have any idea why happen this?