Display Selected value with Html.DropDownList in ASP.Net MVC


I wrote ToastManager about two months ago, and not try to covert it into ASP.Net. What ToastManager does is to collect people's toast order for the morning toast run.

Why? I just think MVC is cool. It separate programming logic into 3 different layers nicely.

The first problem I encountered in writing MVC code was to find a way to display a dropdownlist with a selected value. It is funny. I feel like I am a complete novice when I use MVC.

Simply, you need to pass the selected value in creating SelectList. (from ASP.NET MVC Html.DropDownList SelectedValue)

for example,
[sourcecode language="csharp"]
Slices = new SelectList(new int[] {1, 2, 3, 4}, 2);
[[/sourcecode]

The below is the code for my OrderFormViewModel class

[sourcecode language="csharp"]

public Models.Order Order { get; private set; }
public string[] Spread { get; private set; }
public SelectList Slices { get; private set; }

public OrderFormViewModel(Order order)
{
Order = order;
Spread = new[] { "Butter", "Jam", "Peanut Butter", "Bovril", "Honey", "Marmalade", "Marmite", "Vegemite" };
int[] sliceList = new int[] {1, 2, 3, 4};
Slices = new SelectList(sliceList, Order.Slice);
}

[/sourcecode]