Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中将数据库值绑定到DropDownList_C#_Html_Drop Down Menu_Controller - Fatal编程技术网

C# 如何在C中将数据库值绑定到DropDownList

C# 如何在C中将数据库值绑定到DropDownList,c#,html,drop-down-menu,controller,C#,Html,Drop Down Menu,Controller,我有一个sql server表ProjectDetails。该表有以下列IDPK、ProjectID和Project。我需要将projectd和Project的值填充到2个不同的下拉列表中 型号: public class ViewModel { public int ID { get; set; } public string Project { get; set; } public string ProjectID { get; set

我有一个sql server表ProjectDetails。该表有以下列IDPK、ProjectID和Project。我需要将projectd和Project的值填充到2个不同的下拉列表中

型号:

public class ViewModel
    {
        public int ID { get; set; }
        public string Project { get; set; }
        public string ProjectID { get; set; }

    }
控制器:

 [ActionName("DetailsForm")]
    [HttpGet]
    public ActionResult DetailsForm()
    {
        try
        {
            IEnumerable<ViewModel> dropConfig = new List<ViewModel>();

            IEnumerable<ViewModel> selectList = floorService.DropDownList();

            ViewModel model = new ViewModel()
         {

             dropConfig = selectList,


         };

            return View("DetailsForm",model);

        }
我很难弄清楚在存储库代码下应该放什么,在那里我需要执行存储的proc GetProjectDetails,它将为我提供表ProjectDetails中的数据
请帮我举一些例子,让我学会继续解决问题。提前感谢。

在ASP.NET MVC中,Dropdownlist是使用SelectList Helper类生成的,因此您需要使用SelectList项,并可以使用ViewBag发送值

ViewBag.SelectList= new SelectList(_classObject.YourClassMethod(), "DropDownListValue", "DropDownListText");
在Razor视图中,您可以使用Razor HTML帮助器直接添加它

@Html.DropDownList("SelectList", ViewBag.SelectList as SelectList,"Select Item", new {@class = "form-control" })
另一方面,如果您想使用强类型

@Html.DropDownListFor(model => model.SelectList, ViewBag.SelectListas SelectList, "Select Item", new { @class = "form-control" })

在ASP.NET MVC中,Dropdownlist是使用SelectList Helper类生成的,因此您需要使用SelectList项,并且可以使用ViewBag发送值

ViewBag.SelectList= new SelectList(_classObject.YourClassMethod(), "DropDownListValue", "DropDownListText");
在Razor视图中,您可以使用Razor HTML帮助器直接添加它

@Html.DropDownList("SelectList", ViewBag.SelectList as SelectList,"Select Item", new {@class = "form-control" })
另一方面,如果您想使用强类型

@Html.DropDownListFor(model => model.SelectList, ViewBag.SelectListas SelectList, "Select Item", new { @class = "form-control" })

你发布的代码毫无意义。ViewModel类没有名为dropConfig的属性,但在控制器中有该代码。您的代码将无法编译。请发布将编译的代码,并告诉我们您对哪个特定部分有问题。您发布的代码毫无意义。ViewModel类没有名为dropConfig的属性,但在控制器中有该代码。您的代码将无法编译。请发布将编译的代码,并告诉我们您对哪个特定部分有问题。我的问题是如何从数据库中获取值。应该有executereader什么的对吗?我有一个包含select语句的存储过程。您可以使用实体框架、Drapper或ADO.NET获取这些数据。我的问题是如何从数据库中获取值。应该有executereader什么的对吗?我有一个包含select语句的存储过程。您可以使用实体框架、Drapper或ADO.NET获取这些数据