Is it possible to use multiple view model in same view?
I was trying to use a viewmodel using @model attribute
But i am stuck if i can use a second viewModel, where i put the second one?
Is it possible to use multiple view model in same view?
I was trying to use a viewmodel using @model attribute
But i am stuck if i can use a second viewModel, where i put the second one?
You can't send two view models in the same View.
As a quick workaround, you can create another class that wraps all the models you want to send in the view. By this way, you will benefit from a strongly typed view.
public class Foo
{
public int ID { get; set; }
public string Name { get; set; }
}
public class Bar
{
public int ID { get; set; }
public string Name { get; set; }
}
public class FooBar
{
public Foo Foo { get; set; }
public Bar Bar { get; set; }
}
Inside your action method:
var foo = new Foo
{
ID = 1,
Name = "Foo"
};
var bar = new Bar
{
ID = 2,
Name = "Bar"
};
var fooBar = new FooBar
{
Foo = foo,
Bar = bar
};
return View(fooBar);