I'm curious about binding properties from a ViewModel to another Model.
ViewModel:
- public class StoreIndexData
-
- {
-
- public Store Stores { get; set; }
-
- public District Districts { get; set; }
-
- public Department Departments { get; set; }
-
- }
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:
- @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:
- <form asp-action="Create" role="form">@*
- @await Html.PartialAsync("_ModalHeader", new ModalHeader
- { Heading = String.Format("{0} Store", @Model.Stores.StoreID == 0 ? "Add" : "Edit") })*@
-
- <div asp-validation-summary="ModelOnly" class="text-danger"></div>
- <div class="modal-body form-horizontal">
- <div class="form-group">
- <label asp-for="Departments.DepartmentID" class="col-md-2 control-label"></label>
- <div class="col-md-10">
- <select asp-for="Departments.DepartmentID" class="form-control"
- asp-items="@(new SelectList(@ViewBag.ListofDepartment,"DepartmentID","DepartmentName"))"></select>
- </div>
- </div>
- <div class="form-group">
- <label class="col-md-2 control-label">District</label>
- <div class="col-md-10">
- <select class="form-control" id="DistrictID" name="DistrictID" asp-for="Departments.DistrictID"
- asp-items="@(new SelectList(string.Empty,"DistrictID","DistrictName"))"></select>
- </div>
- </div>
- <div class="form-group">
- <label asp-for="Stores.StoreChainID" class="col-md-2 control-label"></label>
- <div class="col-md-10">
- <select asp-for="Stores.StoreChainID" class="form-control" asp-items="ViewBag.ChainList"></select>
- <span asp-validation-for="Stores.StoreChainID" class="text-danger"></span>
- </div>
- </div>
- <div class="form-group">
- <label asp-for="Stores.StoreName" class="col-md-2 control-label"></label>
- <div class="col-md-10">
- <input asp-for="Stores.StoreName" class="form-control" />
- <span asp-validation-for="Stores.StoreName" class="text-danger"></span>
- </div>
- </div>
- <div class="form-group">
- <label asp-for="Stores.StoreAddress" class="col-md-2 control-label"></label>
- <div class="col-md-10">
- <input asp-for="Stores.StoreAddress" class="form-control" />
- <span asp-validation-for="Stores.StoreAddress" class="text-danger"></span>
- </div>
- </div>
- <div class="form-group">
- <label asp-for="Stores.StoreArea" class="col-md-2 control-label"></label>
- <div class="col-md-10">
- <input asp-for="Stores.StoreArea" class="form-control" />
- <span asp-validation-for="Stores.StoreArea" class="text-danger"></span>
- </div>
- </div>
- @await Html.PartialAsync("_ModalFooter", new ModalFooter { })
- </div>
- </form>
The trick must come here I suposse, in the Post method:
- [HttpPost, ActionName("Create")]
-
- [ValidateAntiForgeryToken]
-
- public async Task Create(int? id, [Bind("StoreID,DistrictID,StoreChainID,StoreName,StoreAddress,StoreArea")]Store store)
-
- {
-
- if (ModelState.IsValid)
-
- {
-
- bool isNew = !id.HasValue;
-
- if (isNew)
-
- {
-
- _context.Add(store);
-
- await _context.SaveChangesAsync();
-
- return RedirectToAction("Index");
-
- }
-
- var storetoupdate = await _context.Stores.SingleOrDefaultAsync(s => s.StoreID == id);
-
- }
-
- ViewData["DistrictID"] = new SelectList(_context.Districts, "DistrictID", "DistrictID", store.DistrictID);
-
- ViewData["StoreChainID"] = new SelectList(_context.StoreChains, "StoreChainID", "StoreChainID", store.StoreChainID);
-
- return RedirectToAction("Index");
-
- }
First, I would need to change:
- [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
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,