I'm working on a project in Microsoft Visual Studio, ASP. NET MVC5 (c#) where i would like to display a list of prices in a Strongly Typed View with html helpers. Since i need (not in this case however) to access multiple models within a single view for this project, i've created a ViewModel, like so:
public class ReservationViewModel
{
public Customer Customer { get; set; }
public Invoice Invoice { get; set; }
public OrderItem OrderItem { get; set; }
public Dictionary<string, decimal> Pricelist { get; set; }
public ReservationViewModel() {
Customer = new Customer();
Invoice = new Invoice();
OrderItem = new OrderItem();
Pricelist = new Dictionary<string, decimal>();
}
public void PopulatePricelist()
{
Pricelist.Add("Select specie", 0);
Pricelist.Add("Bird spider", 90);
Pricelist.Add("Buggie", 70);
Pricelist.Add("Canary", 60);
Pricelist.Add("Chinchilla", 70);
Pricelist.Add("Cat", 140);
Pricelist.Add("Dog", 200);
Pricelist.Add("Guinea pig", 80);
Pricelist.Add("Hamster", 80);
Pricelist.Add("Iguana", 160);
Pricelist.Add("Rabbit", 90);
Pricelist.Add("Snake", 80);
}
}
As you can see, i've named my Dictionary "Pricelist". What i want to do next is to iterate through that list and display all entries in my Strongly Typed View. Within this View, i reference my ViewModel like this:
@model PetsParadise.ViewModels.ReservationViewModel
I've read that the best way of looping over a dictionary is to use this approach:
@foreach (KeyValuePair<string, decimal> entry in Pricelist) {
<tr>
<td>@entry.Value</td>
</tr>
My problem is that 'Pricelist' catches an error stating: "The name "Pricelist" does not exist in the current context" I'm very new to MVC 5 and C# in general, so i appologize for this inquiry as i have a feeling it's pretty silly, but i've been looking everywhere so far and can't seem to find an answer. Note that i've also implemented an SQL Server Management Studio 2008 Database for this project, which means that the classes Customer, Invoice and OrderItem (seen within the ViewModel) are not located within the 'Models' folder. I'm not sure if this is of any relevance.
You need to to qualify your Pricelist member with the
Modelproperty.