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; }
- }
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:
- @await Html.PartialAsync("_ModalHeader", new ModalHeader
- { Heading = String.Format("{0} Store", @Model.Stores.StoreID == 0 ? "Add" : "Edit") })*@
- "ModelOnly" class="text-danger">
- class="modal-body form-horizontal">class="form-group">
- ="Departments.DepartmentID" class="col-md-2 control-label">
class="col-md-10">- ="Departments.DepartmentID" class="form-control"
- asp-items="@(new SelectList(@ViewBag.ListofDepartment,"DepartmentID","DepartmentName"))">
class="form-group">- ="col-md-2 control-label">District
class="col-md-10">- ="form-control" id="DistrictID" name="DistrictID" asp-for="Departments.DistrictID"
- asp-items="@(new SelectList(string.Empty,"DistrictID","DistrictName"))">
class="form-group">- ="Stores.StoreChainID" class="col-md-2 control-label">
class="col-md-10">- ="Stores.StoreChainID" class="form-control" asp-items="ViewBag.ChainList">
- for="Stores.StoreChainID" class="text-danger">
class="form-group">- ="Stores.StoreName" class="col-md-2 control-label">
class="col-md-10">- for="Stores.StoreName" class="form-control" />
- for="Stores.StoreName" class="text-danger">
class="form-group">- ="Stores.StoreAddress" class="col-md-2 control-label">
class="col-md-10">- for="Stores.StoreAddress" class="form-control" />
- for="Stores.StoreAddress" class="text-danger">
class="form-group">- ="Stores.StoreArea" class="col-md-2 control-label">
class="col-md-10">- for="Stores.StoreArea" class="form-control" />
- for="Stores.StoreArea" class="text-danger">
- @await Html.PartialAsync("_ModalFooter", new ModalFooter { })
- [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
- 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,
Replies
Know the answer? Post it — somebody with the same question will find it here.