ASP.NET MVC DropDownList UI Template

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

Instead of displaying ShopID as a whole in my drop-down list, I want to be able to display ShopName from my Shop class, but when I return back to the controller method, I still want the Order class to have the selected ShopID.

How can I do it?

+3
source share
1 answer

To get a drop-down list for displaying text elements, but return a numeric identifier, you must pass it a SelectList through your model.

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

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

Then, in your opinion:

<%= Html.DropDownList("ShopID", Model.Shops) %>
+3
source

Source: https://habr.com/ru/post/1719555/


All Articles