I'm curious about binding properties from a ViewModel to another Model.

ViewModel:

 

  1. public class StoreIndexData  
  2.   
  3. {  
  4.   
  5. public Store Stores { get; set; }  
  6.   
  7. public District Districts { get; set; }  
  8.   
  9. public Department Departments { get; set; }  
  10.   
  11. }  

 

Important note: I use a ViewModel because I need these models together in order to make a cascading dropdownlist to work, however this model is not added in the DB context.

In the view, the user can pickup a Department, then the dropdownlist for District populates, and finally the user adds the information of the Store.

Problem: The problem comes when I want to save the properties the user wrote inside the Store model, since the view is constructed using:

 

  1. @model Application.Models.ApplicationviewModels.StoreIndexData  

 

and the inputs point to this model.

Relevant info:

This is my view. I access each model from the ViewModel (Department, District, Stores) in order to get each property:

  1. "Create" role="form">@*  
  2.         @await Html.PartialAsync("_ModalHeader"new ModalHeader  
  3.     { Heading = String.Format("{0} Store", @Model.Stores.StoreID == 0 ? "Add" : "Edit") })*@  
  4.   
  5.     "ModelOnly" class="text-danger">
  
  •     class="modal-body form-horizontal">  
  •         class="form-group">  
  •             for="Departments.DepartmentID" class="col-md-2 control-label">  
  •             class="col-md-10">  
  •                 for="Departments.DepartmentID" class="form-control"  
  •                         asp-items="@(new SelectList(@ViewBag.ListofDepartment,"DepartmentID","DepartmentName"))">  
  •             
  •   
  •