Reputation: 45
I would first like to take I am a novice at this, my background is asp web forms and I am trying to update my skill set to the latest development language.
I have a view which needs to access multiple modules so I created a viewmodel and pass that to the view. My problem is when I try to access a list from the view model to populate a select (dropdown). I keep getting an error. Below is my viewmodel: -
public class CustomerDetailsViewModel
{
public Customer customer { get; set; }
public IEnumerable<Lookups> titles { get; set; }
public List<Lookups> ePCs { get; set; }
public List<Lookups> jobStages { get; set; }
}
Below is my code for the select control. Where I try to access the titles list I get an error.
<div class="form-group">
<label asp-for="customer.Title_Id" class="col-sm-2 col-md-4 col-form-label">Title</label>
<select app-for="Title_Id" asp-items="@(new SelectList(titles,"PK_Id","Description"))"></select>
</div>
Upvotes: 3
Views: 1946
Reputation: 4899
Since you have created a view model, I assume your view has the @model
directive:
@model CustomerDetailsViewModel
When you pass the view model from the controller to the view, it is accessible in the view through the Model
property.
The asp-for
tag helper does not need the Model
property, it is implicit. On the other hand, asp-items
does not work that way, it needs the Model
prefix:
<select asp-for="customer.Title_Id"
asp-items="@(new SelectList(Model.titles,"PK_Id","Description"))">
</select>
Also, since all your view model properties are public
, the naming convention is to use PascalCase
names:
public class CustomerDetailsViewModel
{
public Customer Customer { get; set; }
public IEnumerable<Lookups> Titles { get; set; }
public List<Lookups> EPCs { get; set; }
public List<Lookups> JobStages { get; set; }
}
Upvotes: 4