3

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?

1
  • i'm using the new preview of mvc 2 Commented Oct 7, 2009 at 20:05

1 Answer 1

3

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) %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.