Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.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/2/.net/24.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# 在ASP.NET MVC中填充DropDownlist_C#_.net_Asp.net_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

C# 在ASP.NET MVC中填充DropDownlist

C# 在ASP.NET MVC中填充DropDownlist,c#,.net,asp.net,asp.net-mvc,asp.net-mvc-3,C#,.net,Asp.net,Asp.net Mvc,Asp.net Mvc 3,我正在学习ASP.NETMVC。我在处理dropdownlist时遇到了一个小问题 这就是我在控制器中所做的 List<Int32> myList = new List<Int32>(); var a = (from m in datacontext.services select m.ServiceID); myList = a.ToList(); ViewData["ServiceID"] = new SelectList((IEnu

我正在学习ASP.NETMVC。我在处理dropdownlist时遇到了一个小问题

这就是我在控制器中所做的

List<Int32> myList = new List<Int32>();
var a = (from m in datacontext.services
                 select m.ServiceID);
myList = a.ToList();
ViewData["ServiceID"] = new SelectList((IEnumerable<SelectListItem>)
                                a.ToList(), "ServiceID", "ServiceID");
这会导致一个错误

无法强制转换“System.Collections.Generic.List
1[System.Int32]类型的对象”
键入“System.Collections.Generic.IEnumerable
1[System.Web.Mvc.SelectListItem]”

如何解决这个问题


用查询填充dropdownlist的最佳方法是什么?

强制转换中有一个错误

要修复此问题,您可以执行以下操作:

var a = datacontext.services.Select(arg => new { ServiceID = arg.ServiceID }).ToList();
ViewData["ServiceID"] = new SelectList(a, "ServiceID", "ServiceID");
或者这个:

var a = datacontext.services.Select(arg => arg.ServiceID).ToList();
ViewData["ServiceID"] = new SelectList(a);
在代码中,
a
是整数列表。它不能强制转换到
SelectListItem
列表中

旁注:
myList
变量未在您的代码中使用。

您需要将
Int32
列表转换为
SelectListItem
列表/IEnumerable。您无法以直接转换的方式执行此操作(这是您正在尝试执行的操作,也是您的错误表示)

通过更改select返回类型,可以调整LINQ查询以返回
IEnumerable

var a = (from m in datacontext.services
                 select new SelectListItem{ Text = m.ServiceID.ToString(), Value = m.ServiceID.ToString() });
然后,您可以通过以下方式将其加载到
选择列表中:

ViewData["ServiceID"] = new SelectList(a);

此外,如果要使用文本项填充下拉列表,可以执行以下操作:

控制器:

public ActionResult Create()
{
    var viewModel = new Store();
    var storeTypes = new SelectList(
                        (   
                            from t in _db.StoreTypes
                            select new SelectListItem
                            {
                                Text  = t.StoreTypeName,
                                Value = SqlFunctions.StringConvert((double)t.StoreTypeID) //convert int to string (yikes)
                            }
                        )
                        , "Value", "Text");

    ViewData["StoreTypes"] = storeTypes;    
     return View(viewModel);
} 
视图:


--%>
public ActionResult Create()
{
    var viewModel = new Store();
    var storeTypes = new SelectList(
                        (   
                            from t in _db.StoreTypes
                            select new SelectListItem
                            {
                                Text  = t.StoreTypeName,
                                Value = SqlFunctions.StringConvert((double)t.StoreTypeID) //convert int to string (yikes)
                            }
                        )
                        , "Value", "Text");

    ViewData["StoreTypes"] = storeTypes;    
     return View(viewModel);
} 
<div class="class">
    <%: Html.DropDownList("StoreTypeID", (SelectList)ViewData["StoreTypes"]) %>
    <%--<%: Html.DisplayTextFor(model => model.StoreTypeID) %>--%>
</div>