You need to create the DeviceEditViewModel
like below, then use it as strongly typed view in your Index
View, then pass the object DropDownListViewModel
to your partial view.
public class DeviceEditViewModel
{
public Device dev { get; set; }
public DropDownListViewModel DropDownListViewModels { get; set; }
}
public class DropDownListViewModel
{
public int SelectedManufactor { set; get; }
public IEnumerable<SelectListItem> ManufactorListItems { get; set; }
public int SelectedCategory { set; get; }
public IEnumerable<SelectListItem> CategoriesListItems { get; set; }
}
Pass the object DropDownListViewModel
to your partial view
@{ Html.RenderPartial("_Create", Model.DropDownListViewModels);}
Change your Create
action method like below:
public ActionResult Create()
{
var vm = new DeviceEditViewModel
{
DropDownListViewModels = new DropDownListViewModel()
{
CategoriesListItems = repo.GetCategories().Select(x => new SelectListItem
{
Value = x.ID.ToString(),
Text = x.Name
}),
ManufactorListItems = repo.GetManufactors().Select(x => new SelectListItem
{
Value = x.ID.ToString(),
Text = x.Name
})
}
};
return View(vm);
}
Your partial view:
@model DropDownListViewModel
@Html.DropDownListFor(n => n.SelectedManufactor, Model.ManufactorListItems)
@Html.DropDownListFor(n => n.SelectedCategory, Model.CategoriesListItems)