简体   繁体   中英

C# MVC populating drop down value from DB

I am struggling to understand MVC. I have created couple of pages that work. Now I created a new page which has a dropdownlist which needs to populate from a table called accounts(coloumn is called accountNumber)

However there is a relationship with that table and table called customer(customerid).

In essence the user whose logged in has a customerid.

what is the easiest way of doing this? when I enter @Html.DropDownListFor(u => u.

none of tables I need pop up

The reason the tables aren't showing up when you do @Html.DropDownListFor(u => u. is that in this case u is the model which you have passed into the view. You need to populate the model with the entries that you wish to display in the drop down list. A good example of this is available at http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc

Update

This SO post may also be of use, the OP has provided their code for how they retrieve the entries from the database (see getAllCurrencies() method); MVC populating drop down list from database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM