You can do it like so:
This is the InsertOrder class:
public class InsertOrder
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public List<SelectListItem> SelectSource { get; set; }
public InsertOrder()
{
SelectSource = new List<SelectListItem>();
using (
var connection =
new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
{
using (var command = new SqlCommand("SELECT CustomerID,FirstName FROM customers", connection))
{
connection.Open();
using (var dataReader = command.ExecuteReader())
{
while (dataReader.Read())
{
var ID = new SelectListItem
{
Value = dataReader["CustomerID"].ToString(),
Text = dataReader["FirstName"].ToString()
};
SelectSource.Add(ID);
}
}
}
}
}
}
@Html.DropDownFor(model => model.CustomerId, Model.SelectSource)
Now since you have a customer Id, you're better off NOT having the customer name field in this model, but if you just can't part with it, you can pull the name out via jQuery:
<script type="text/javascript">
$('#CustomerId').select(function(){
$('#CustomerName').val($('#CustomerId option:selected').text());
});
</script>