TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Luis Alberto Delgado de la Flor
NA
127
23.1k
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:
public
class
StoreIndexData
{
public
List
Stores {
get
;
set
; }
public
List
Departments {
get
;
set
; }
}
This is my Index method where I initialize the models and send it to the view:
public
async Task
Index()
{
var viewModel =
new
StoreIndexData();
viewModel.Stores = await _context.Stores
.Include(c => c.StoreChains)
.Include(d => d.Districts)
.AsNoTracking().ToListAsync();
viewModel.Departments =
new
List
();
viewModel.Departments = (from department
in
_context.Departments
select department).ToList();
viewModel.Departments.Insert(0,
new
Department { DepartmentID = 0, DepartmentName =
"Select"
});
ViewBag.ListofDepartment = viewModel.Departments;
return
View(viewModel);
}
Then, in my view, I have this segment to show a dropdownlist for the department class:
=
"DepartmentName"
class
=
"control-label"
>
class
=
"form-control"
asp-
for
=
"DepartmentID"
asp-items=
"@(new SelectList(ViewBag.ListofDepartment,"
DepartmentID
","
DepartmentName
"))"
>
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?)
+
#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?)
+
#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:
for
=
"Departments.DepartmentName"
class
=
"control-label"
>
BUT, I don't have a singular Department, so, If i changed the ViewModel like this:
public
class
StoreIndexData
{
public
List<Store> Stores {
get
;
set
; }
public
List<Department> Departments {
get
;
set
; }
public
string
DepartmentName {
get
;
set
; }
public
int
DepartmentID {
get
;
set
; }
}
and change the view back to this:
<label asp-
for
=
"DepartmentName"
class
=
"control-label"
></label>
<select
class
=
"form-control"
asp-
for
=
"DepartmentID"
asp-items=
"@(new SelectList(ViewBag.ListofDepartment,"
DepartmentID
","
DepartmentName
"))"
></select>
It works, but I'm not sure if this is the correct procedure.
Reply
Answers (
1
)
best way to work with admin template for different user role
MVC 5 EMAIL CONFIRMATION