views:

134

answers:

3

Hi

I have a view that is not strongly typed. However I have in this view a partial view that is strongly typed.

How do I do I pass the model to this strongly typed view?

I tried something like

 public ActionResult Test()
        {
              MyData = new Data();
              MyData.One = 1;
              return View("Test",MyData)
        }

In my TestView

<% Html.RenderPartial("PartialView",Model); %>

This give me a stackoverflow exception. So I am not sure how to pass it on. Of course I don't want to make the test view strongly typed if possible as what happens if I had like 10 strongly typed partial views in that view I would need like some sort of wrapper.

+2  A: 

Check out my post from last week: http://stackoverflow.com/questions/2745747/problem-with-strongly-typed-partial-view/2745808#2745808

hunter
So you have to make the view strongly typed then? and use a wrapper.
chobo2
A: 

Put the object required by the partial into Viewdata and use ist in the view as input for the partial.

public ActionResult Test()
        {
              ViewData["DataForPartial"] = new PartialDataObject();
              return View("Test")
        }

In the view use:

<% Html.RenderPartial("PartialView",ViewData["DataForPartial"]); %>

But anyway: There is no reason no to have a stronly typed view.

Malcolm Frexner
The problem is not it being the view being a strongly typed view is the fact that if you have more then one partial view in that view you can't make it a strongly typed view to fit all those cases unless you make some sort of wrapper or I guess use ViewData.
chobo2
A: 

You should extend your model so that it can provide all necessary fields for the view (this is called ViewModel) or you provide them seperately with ViewData.

 public ActionResult Test()
        {
              MyData = new Data();
              MyData.One = 1;
              ViewData["someData"]=MyData;
              return View();
        }

then:

<% Html.RenderPartial("PartialView",ViewData["someData"]); %>

ViewData is a nice losely typed dictionary

Ufuk Hacıoğulları
Then I would have to cast it in the partialview right?
chobo2
What do you mean?If your partial view is strongly-typed with the MyData class, line above will render your view without a problem. As you can see, you send more than one models into your view by ViewData because it is not strongly typed, then you pull your models for each strongly-typed PartialView.
Ufuk Hacıoğulları