Create a view model to combine the models for your partial views:
public class TestIndexViewModel
{
public Test1ViewModel Test1 { get; set; }
public Test2ViewModel Test2 { get; set; }
}
Change your controller to build the combined view-model, rather than calling
PartialView
and throwing the results away:
public class TestController : Controller
{
public ActionResult Index()
{
var model = new TestIndexViewModel
{
Test1 = CreatePartial1ViewModel(),
Test2 = CreatePartial2ViewModel(),
};
return View(model);
}
private Test1ViewModel CreatePartial1ViewModel() => new Test1ViewModel
{
id = 1,
Name = "Name1",
};
private Test2ViewModel CreatePartial2ViewModel() => new Test2ViewModel
{
id = 1,
Name = "Name2",
};
}
Change your index view to pass the correct viewmodel to the partial views:
@model MyApp.ViewModels.TestIndexViewModel
@{
ViewBag.Title = "index";
}
<div class="container">
<div class="row">
@Html.Partial("Partial_1", Model.Test1)
@Html.Partial("Partial_2", Model.Test2)
</div>
</div>