Select Tag Helper: asp-for

Jun 23 2017 8:11 PM
Hello,
 
I'm constructing a view where I render the complete list of the classes grouped on a ViewModel. These are Stores and Departments (as cities).
 
I have a problem when I'm constructing the view, since the segment where I put the 'asp-action' tag helper.
 
This is my viewmodel called StoreIndexData:
  1.   public class StoreIndexData  
  2.   {  
  3. public List Stores { getset; }  
  4. public List Departments { getset; }  
  5.   }  
This is my Index method where I initialize the models and send it to the view:
 
  1. public async Task Index()  
  2.         {  
  3.             var viewModel = new StoreIndexData();  
  4.             viewModel.Stores = await _context.Stores  
  5.                 .Include(c => c.StoreChains)  
  6.                 .Include(d => d.Districts)  
  7.                 .AsNoTracking().ToListAsync();  
  8.   
  9.             viewModel.Departments = new List();  
  10.             viewModel.Departments = (from department in _context.Departments  
  11.                               select department).ToList();  
  12.             viewModel.Departments.Insert(0, new Department { DepartmentID = 0, DepartmentName = "Select" });  
  13.             ViewBag.ListofDepartment = viewModel.Departments;  
  14.   
  15.             return View(viewModel);  
  16.         }  
Then, in my view, I have this segment to show a dropdownlist for the department class:
  1.                 
  2.                         class="form-control"  
  3.                         asp-for="DepartmentID"  
  4.                         asp-items="@(new  SelectList(ViewBag.ListofDepartment,"DepartmentID","DepartmentName"))">  
  5.             
  
 When I debug the application, I get the following error:
 
  • 'StoreIndexData' does not contain a definition for 'DepartmentName' and no extension method 'DepartmentName' accepting a first argument of type 'StoreIndexData' could be found (are you missing a using directive or an assembly reference?)

    1. #pragma warning restore 1998
  • 'StoreIndexData' does not contain a definition for 'DepartmentID' and no extension method 'DepartmentID' accepting a first argument of type 'StoreIndexData' could be found (are you missing a using directive or an assembly reference?)

    1. #pragma warning restore 1998
 
So, which would be the correct way to use the tag helpers in this case?
 
I tried to debug the application deleting the asp-for tag helper and it runs as expected, but I don't believe this is the right thing to do. Any explanation on why is it important to use this tag helper?
 
Thanks! 
 
 _EDIT:
 
Ok, so first, since I'm using a ViewModel, I would need to access the model that contains this property, which is Department, like this:
 
  1. for="Departments.DepartmentName" class="control-label">    
BUT, I don't have a singular Department, so, If i changed the ViewModel like this: 
  1.   public class StoreIndexData  
  2.   {  
  3. public List<Store> Stores { getset; }  
  4. public List<Department> Departments { getset; }  
  5. public string DepartmentName { getset; }  
  6. public int DepartmentID { getset; }  
  7.   }  
and change the view back to this: 
  1. <label asp-for="DepartmentName" class="control-label"></label>  
  2.                 <select   
  3.                         class="form-control"  
  4.                         asp-for="DepartmentID"  
  5.                         asp-items="@(new  SelectList(ViewBag.ListofDepartment,"DepartmentID","DepartmentName"))"></select>  
 It works, but I'm not sure if this is the correct procedure.
 

Answers (1)