Hello,
In my project the company has a set of Stores where it operates. Each Store is located in a District and every District belongs to a Department (or city).
I'm trying to construct a view where I can show the Store properties (in a table) and I'm trying to add a cascading dropdownlist for the Departments and the Districts.
Here are the models so you can see the relationship between them:
Stores:
- public class Store
- {
- [Key]
- public int StoreID { get; set; }
- [Display(Name = "Distrito")]
- public int DistrictID { get; set; }
- [Display(Name = "Cadena")]
- public int StoreChainID { get; set; }
- [Required]
- [StringLength(60)]
- [Display(Name = "Nombre")]
- public string StoreName { get; set; }
- [StringLength(60)]
- [Display(Name = "Dirección")]
- public string StoreAddress { get; set; }
- [Display(Name = "Área")]
- public int StoreArea { get; set; }
- public virtual ICollection
Machines { get; set; } - public virtual District Districts { get; set; }
- public virtual StoreChain StoreChains { get; set; }
- public virtual LocalExpenses LocalExpenses { get; set; }
District:
- public class District
- {
- [Key]
- public int DistrictID { get; set; }
- public int DepartmentID { get; set; }
- [StringLength(30)]
- public string DistrictName { get; set; }
- public virtual ICollection
Stores{ get; set; } - public virtual Department Departments { get; set; }
Department:
- public class Department
- {
- [Key]
- public int DepartmentID { get; set; }
- [StringLength(30)]
- public string DepartmentName { get; set; }
- [NotMapped]
- public int DistrictID {get;set;}
- public virtual ICollection
Districts { get; set; } - }
First action:
- public async Task
Index() - {
- var plataformaContext = _context.Stores.Include(s => s.Districts).Include(c => c.StoreChains);
- List
departmentlist = new List (); - //Getting the Data from DB
- departmentlist = (from department in _context.Departments select department).ToList();
- //Inserting an additional item into the list: The "Select"
- departmentlist.Insert(0, new Department { DepartmentID = 0, DepartmentName = "Select" });
- ViewBag.ListofDepartment = departmentlist;
- return View(await plataformaContext.ToListAsync());
- }
- [HttpPost]
- public IActionResult Index(Department objdepartment, IFormCollection formCollection)
- {
- //Validación
- if (objdepartment.DepartmentID == 0)
- {
- ModelState.AddModelError("", "Select Department");
- }
- else if (objdepartment.DistrictID == 0)
- {
- ModelState.AddModelError("", "Select District");
- }
- //Getting the selected value
- var DistrictID = HttpContext.Request.Form["DistrictID"].ToString();
- //Sending the data back to ViewBag after Posting Form
- List
departmentList = new List (); - departmentList = (from department in _context.Departments
- select department).ToList();
- departmentList.Insert(0, new Department { DepartmentID = 0, DepartmentName = "Select" });
- // Assigning departmentList to ViewBag
- ViewBag.ListofDepartment = departmentList;
- return View(objdepartment);
- }
Finally, here is the Index view:
- @model List
- @{
- ViewData["Title"] = "Index";
- }
Index
- "Create">Create New
class="table">
- Nombre de Tienda @*@Html.DisplayNameFor(model => model.Stores.StoreName)*@
- Address @*@Html.DisplayNameFor(model => model.Stores.StoreAddress)*@
- Area @*@Html.DisplayNameFor(model => model.Stores.StoreArea)*@
- Distrito @*@Html.DisplayNameFor(model => model.Stores.Districts)*@
- Cadena @*@Html.DisplayNameFor(model => model.Stores.StoreChains)*@
- @foreach (var item in Model) {
- @Html.DisplayFor(modelItem => item.StoreName)
- @Html.DisplayFor(modelItem => item.StoreAddress)
- @Html.DisplayFor(modelItem => item.StoreArea)
- @Html.DisplayFor(modelItem => item.Districts.DistrictID)
- @Html.DisplayFor(modelItem => item.StoreChains.StoreChainID)
- "Edit" asp-route-id="@item.StoreID">Edit |
- "Details" asp-route-id="@item.StoreID">Details |
- "Delete" asp-route-id="@item.StoreID">Delete
- }
- asp-action="Index" method="post" class="form-horizontal" role="form">
- class="form-group">class="row">class="alert-danger" aspp-validation-summary="ModelOnly">class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
- ="DepartmentName" class="control-label">
- ="DepartmentID"
- class="form-control"
- asp-items="@(new SelectList(ViewBag.ListofDepartment,"DepartmentID","DepartmentName"))">
Ok, so, yes, this is not working because I'm using a Store model in the Index:
- @model List
- public class StoreLocationViewModel
- {
- public Store Stores { get; set; }
- public Department Departments { get; set; }
- }
- @model List
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List`1[Application.Models.Store]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.List`1[Application.Models.StoreLocationViewModel]'.
The error is now in the Controller, since I'm sending this:
- var plataformaContext = _context.Stores.Include(c => c.StoreChains).Include(s => s.Districts)
- .ThenInclude(d=>d.Departments);
I'm very experienced on this, so I thank you for the time to read this. Any help is welcome as always!
Thanks,
Regards.
Manikandan MurugesanPosted Jun 23, 2017, 3:40 AM