views:

272

answers:

1

Hi,

I have a ui template for the property ShopID in my Order class.

Instead of displaying the ShopID as an integer in my dropdown I want to be able to display the ShopName from my Shop class but when I post back to the controller method I still want the Order class to have the selected ShopID.

How would I do this?

+2  A: 

To get a dropdown to display text items but return a numeric ID, you have to pass it a SelectList via your model.

public SelectList Shops
{
    get
    {
        var list = 
            from shop in myDataContext.Shops
            Select shop;

        return new SelectList(list, "ShopID", "ShopDescription");
    }
}

Then, in your view:

<%= Html.DropDownList("ShopID", Model.Shops) %>
Robert Harvey