Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Model from a view to a partial view?

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.

like image 774
chobo2 Avatar asked May 05 '10 18:05

chobo2


1 Answers

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

like image 67
Ufuk Hacıoğulları Avatar answered Oct 14 '22 00:10

Ufuk Hacıoğulları