简体   繁体   English

C#MVC从数据库填充下拉值

[英]C# MVC populating drop down value from DB

I am struggling to understand MVC. 我正在努力了解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) 现在,我创建了一个新页面,该页面具有一个下拉列表,需要从一个名为accounts(表的名称为accountNumber)的表中进行填充

However there is a relationship with that table and table called customer(customerid). 但是,与该表和表之间存在一种关系,称为customer(customerid)。

In essence the user whose logged in has a customerid. 本质上,已登录的用户具有客户ID。

what is the easiest way of doing this? 最简单的方法是什么? when I enter @Html.DropDownListFor(u => u. 当我输入@ 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 在执行@Html.DropDownListFor(u => u.时未显示表格的原因是,在这种情况下, u是您已传递到视图中的模型。您需要使用您输入的条目填充模型希望显示在下拉列表中。http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the -dropdownlist-helper-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); 该SO帖子也可能有用,OP提供了有关如何从数据库中检索条目的代码(请参见getAllCurrencies()方法); MVC populating drop down list from database MVC从数据库填充下拉列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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