3

This maybe really stupid but I just can't figure it out...

So here is the problem. I have a model say A and a model B.

class A
{
    B b = new B();
}

class B
{
    ...
}

Now, I have a ViewModel for B (but not for A).

So, now I have a view storngly typed to A. And it calls another partial view B to render all the properties of B. But now, how to I pass the viewmodel for B to the partial view? I can pass the model by just calling Model.b but not the viewmodel!

tereško
  • 58,060
  • 25
  • 98
  • 150
Aditi
  • 1,188
  • 2
  • 16
  • 44
  • Simple answer is no? I will throw exception. – Satpal Jul 16 '13 at 10:53
  • @Satpal But then what to do? I have to use the viewmodel. – Aditi Jul 16 '13 at 10:55
  • simply you have to map `B` model to `ViewModel`. You must have defined the mapper or converter try to use that. Defined a method in `B` model which converts and returns you `ViewModel` and you can pass that `ViewModel`. – Satpal Jul 16 '13 at 10:57
  • @Satpal Ohk.. that's exactly what I need to do.. But I don't have a mapper or converter defined. And googling it just gives lots of pages about AutoMapper... which I don't want to use. Any example code on this?? – Aditi Jul 17 '13 at 05:12

3 Answers3

1

Include class B as a property in Class A. So now you can use class B as Model.bModel.

class A
{
    public B bModel { get; set; }
}

class B
{

}
ssilas777
  • 9,672
  • 4
  • 45
  • 68
1

You need to extend your one ViewModel so that it includes everything you want to access. Then just access different components of it depending on whether you're in your normal or partial view.

B Cotter
  • 951
  • 5
  • 7
1

You can pass B via the ViewBag. In the controller add

ViewBag.dataB = B;

Then you can render a partial view with a model by using

@Html.Partial("PartialName", ViewBag.dataB)

Or you can add a getter property to access B and render the partial using that.

Mirsha
  • 118
  • 5