无法将类型 'System.Collections.Generic.List<InventoryLocations>' 隐式转换为 'System.Collections.Generic.IEnumerable<SelectListItem>'
Cannot implicitly convert type 'System.Collections.Generic.List<InventoryLocations>' to 'System.Collections.Generic.IEnumerable<SelectListItem>'
我是初学者。我正在研究这个 ASP.NET Core MVC
项目,我试图通过提交我的 form
将我的 ViewModel
传递给我的 POST
方法,以便我可以将数据保存在 Database
。一旦我的 GET
方法完成 运行,我就会看到以下内容 RuntimeBinderException
:
RuntimeBinderException: Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?
查看:
<div class="row">
<div class="col-sm-4">
<div class="input-group fg-float">
<div class="fg-line form-chose">
<label asp-for="LocationName" class="fg-labels" for="locationName"></label>
<select asp-for="LocationName" asp-items="ViewBag.Locations" class="chosen disabledropdowncntrl" data-placeholder="Choose a Location" name="locationName" id="dropDownLocationNames">
<option value=""></option>
</select>
</div>
</div>
</div>
</div>
控制器:
// GET: ~/Inventory/Locate/Id
public IActionResult Locate(int Id)
{
InventoryLocationsHistoryViewModel locationsHistoryVM = new InventoryLocationsHistoryViewModel();
// Populating Locations DropDownList
var locationsList = new List<InventoryLocations>();
locationsList = _context.InventoryLocations.ToList();
var locations = locationsList.OrderBy(l => l.LocationName).Select(us => new SelectListItem { Value = us.LocationId.ToString(), Text = us.LocationName }).ToList();
ViewBag.Locations = locationsList;
...
...
return View(locationsHistoryVM);
}
ViewModel:
namespace BackOffice.Models.CustomModels
{
public class InventoryLocationsHistoryViewModel
{
public string UserId { get; set; }
//public ApplicationUser User { get; set; }
public string OtherUser { get; set; }
public string Comments { get; set; }
public string LocationName { get; set; }
public InventoryLocations Location { get; set; }
public DateTime? FromDate { get; set; }
public DateTime? ToDate { get; set; }
}
}
我做错了什么?
您 ViewBag.Locations 设置不正确。您还可以折叠很多代码,因为您将整个列表放入内存中进行排序和 select 2 列。这将创建一个 select 语句,仅 select 需要 2 列并在 sql 服务器端执行排序。
// GET: ~/Inventory/Locate/Id
public IActionResult Locate(int Id)
{
InventoryLocationsHistoryViewModel locationsHistoryVM = new InventoryLocationsHistoryViewModel();
var locations = new SelectList(_context.InventoryLocations.OrderBy(l => l.LocationName)
.ToDictionary(us => us.LocationId, us => us.LocationName), "Key", "Value");
ViewBag.Locations = locations;
...
...
return View(locationsHistoryVM);
}
我是初学者。我正在研究这个 ASP.NET Core MVC
项目,我试图通过提交我的 form
将我的 ViewModel
传递给我的 POST
方法,以便我可以将数据保存在 Database
。一旦我的 GET
方法完成 运行,我就会看到以下内容 RuntimeBinderException
:
RuntimeBinderException: Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?
查看:
<div class="row">
<div class="col-sm-4">
<div class="input-group fg-float">
<div class="fg-line form-chose">
<label asp-for="LocationName" class="fg-labels" for="locationName"></label>
<select asp-for="LocationName" asp-items="ViewBag.Locations" class="chosen disabledropdowncntrl" data-placeholder="Choose a Location" name="locationName" id="dropDownLocationNames">
<option value=""></option>
</select>
</div>
</div>
</div>
</div>
控制器:
// GET: ~/Inventory/Locate/Id
public IActionResult Locate(int Id)
{
InventoryLocationsHistoryViewModel locationsHistoryVM = new InventoryLocationsHistoryViewModel();
// Populating Locations DropDownList
var locationsList = new List<InventoryLocations>();
locationsList = _context.InventoryLocations.ToList();
var locations = locationsList.OrderBy(l => l.LocationName).Select(us => new SelectListItem { Value = us.LocationId.ToString(), Text = us.LocationName }).ToList();
ViewBag.Locations = locationsList;
...
...
return View(locationsHistoryVM);
}
ViewModel:
namespace BackOffice.Models.CustomModels
{
public class InventoryLocationsHistoryViewModel
{
public string UserId { get; set; }
//public ApplicationUser User { get; set; }
public string OtherUser { get; set; }
public string Comments { get; set; }
public string LocationName { get; set; }
public InventoryLocations Location { get; set; }
public DateTime? FromDate { get; set; }
public DateTime? ToDate { get; set; }
}
}
我做错了什么?
您 ViewBag.Locations 设置不正确。您还可以折叠很多代码,因为您将整个列表放入内存中进行排序和 select 2 列。这将创建一个 select 语句,仅 select 需要 2 列并在 sql 服务器端执行排序。
// GET: ~/Inventory/Locate/Id
public IActionResult Locate(int Id)
{
InventoryLocationsHistoryViewModel locationsHistoryVM = new InventoryLocationsHistoryViewModel();
var locations = new SelectList(_context.InventoryLocations.OrderBy(l => l.LocationName)
.ToDictionary(us => us.LocationId, us => us.LocationName), "Key", "Value");
ViewBag.Locations = locations;
...
...
return View(locationsHistoryVM);
}