【问题标题】:Cannot implicitly convert type 'System.Collections.Generic.List<InventoryLocations>' to 'System.Collections.Generic.IEnumerable<SelectListItem>'无法将类型“System.Collections.Generic.List<InventoryLocations>”隐式转换为“System.Collections.Generic.IEnumerable<SelectListItem>”
【发布时间】:2018-03-23 14:20:35
【问题描述】:

我是初学者。我正在处理这个ASP.NET Core MVC 项目,我试图通过提交我的form 将我的ViewModel 传递给我的POST 方法,以便我可以将数据保存在Database 中。只要我的GET 方法运行完毕,我就会看到以下RuntimeBinderException

RuntimeBinderException:无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换(您是否缺少演员表?

查看:

<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);
    }

视图模型:

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 = 位置;
  • ViewBag.Locations = locationsList更改为ViewBag.Locations = locations
  • @Fran,你介意用这个做一个简短的回答吗?
  • 是的。这对其他人也有帮助。 @弗兰
  • 您正在使用视图模型。您的视图模型应包含属性IEnumerable&lt;SelectListItems&gt; Location(当您有视图模型时,切勿使用ViewBag

标签: asp.net-mvc asp.net-core-mvc viewmodel selectlistitem


【解决方案1】:

您的 ViewBag.Locations 设置不正确。您还可以折叠很多代码,因为您将整个列表放入内存以排序和选择 2 列。这将创建一个仅选择所需的 2 列并在 sql server 端执行排序的 select 语句。

// 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);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 2011-05-14
    相关资源
    最近更新 更多