Bind properties from a ViewModel to another model

Jul 12 2017 12:09 AM

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. <form asp-action="Create" role="form">@*  
  2.         @await Html.PartialAsync("_ModalHeader"new ModalHeader  
  3.     { Heading = String.Format("{0} Store", @Model.Stores.StoreID == 0 ? "Add" : "Edit") })*@  
  4.   
  5.     <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
  6.     <div class="modal-body form-horizontal">  
  7.         <div class="form-group">  
  8.             <label asp-for="Departments.DepartmentID" class="col-md-2 control-label"></label>  
  9.             <div class="col-md-10">  
  10.                 <select asp-for="Departments.DepartmentID" class="form-control"  
  11.                         asp-items="@(new SelectList(@ViewBag.ListofDepartment,"DepartmentID","DepartmentName"))"></select>  
  12.             </div>  
  13.         </div>  
  14.         <div class="form-group">  
  15.             <label class="col-md-2 control-label">District</label>  
  16.             <div class="col-md-10">  
  17.                 <select class="form-control" id="DistrictID" name="DistrictID" asp-for="Departments.DistrictID"  
  18.                         asp-items="@(new SelectList(string.Empty,"DistrictID","DistrictName"))"></select>  
  19.             </div>  
  20.         </div>  
  21.         <div class="form-group">  
  22.             <label asp-for="Stores.StoreChainID" class="col-md-2 control-label"></label>  
  23.             <div class="col-md-10">  
  24.                 <select asp-for="Stores.StoreChainID" class="form-control" asp-items="ViewBag.ChainList"></select>  
  25.                 <span asp-validation-for="Stores.StoreChainID" class="text-danger"></span>  
  26.             </div>  
  27.         </div>  
  28.         <div class="form-group">  
  29.             <label asp-for="Stores.StoreName" class="col-md-2 control-label"></label>  
  30.             <div class="col-md-10">  
  31.                 <input asp-for="Stores.StoreName" class="form-control" />  
  32.                 <span asp-validation-for="Stores.StoreName" class="text-danger"></span>  
  33.             </div>  
  34.         </div>  
  35.         <div class="form-group">  
  36.             <label asp-for="Stores.StoreAddress" class="col-md-2 control-label"></label>  
  37.             <div class="col-md-10">  
  38.                 <input asp-for="Stores.StoreAddress" class="form-control" />  
  39.                 <span asp-validation-for="Stores.StoreAddress" class="text-danger"></span>  
  40.             </div>  
  41.         </div>  
  42.         <div class="form-group">  
  43.             <label asp-for="Stores.StoreArea" class="col-md-2 control-label"></label>  
  44.             <div class="col-md-10">  
  45.                 <input asp-for="Stores.StoreArea" class="form-control" />  
  46.                 <span asp-validation-for="Stores.StoreArea" class="text-danger"></span>  
  47.             </div>  
  48.         </div>  
  49.         @await Html.PartialAsync("_ModalFooter"new ModalFooter { })  
  50.     </div>  
  51. </form>  
 

 

The trick must come here I suposse, in the Post method:

 

  1. [HttpPost, ActionName("Create")]  
  2.   
  3. [ValidateAntiForgeryToken]  
  4.   
  5. public async Task Create(int? id, [Bind("StoreID,DistrictID,StoreChainID,StoreName,StoreAddress,StoreArea")]Store store)  
  6.   
  7. {  
  8.   
  9. if (ModelState.IsValid)  
  10.   
  11. {  
  12.   
  13. bool isNew = !id.HasValue;  
  14.   
  15. if (isNew)  
  16.   
  17. {  
  18.   
  19. _context.Add(store);  
  20.   
  21. await _context.SaveChangesAsync();  
  22.   
  23. return RedirectToAction("Index");  
  24.   
  25. }  
  26.   
  27. var storetoupdate = await _context.Stores.SingleOrDefaultAsync(s => s.StoreID == id);  
  28.   
  29. }  
  30.   
  31. ViewData["DistrictID"] = new SelectList(_context.Districts, "DistrictID""DistrictID", store.DistrictID);  
  32.   
  33. ViewData["StoreChainID"] = new SelectList(_context.StoreChains, "StoreChainID""StoreChainID", store.StoreChainID);  
  34.   
  35. return RedirectToAction("Index");  
  36.   
  37. }  

 

First, I would need to change:

 

  1. [Bind("StoreID,DistrictID,StoreChainID,StoreName,StoreAddress,StoreArea")]Store store)  

 

For the ViewModel StoreIndexData.

But then, I'm lost. Maybe if I put a conditional when

 

  1. if (!ModelState.IsValid)  

 

And since this will be true, initialize a new Store model with the values that are coming from the Post method, but I don't know how to do this.

Thanks in advance for any help, I hope the problem is clear.

Regards,