【发布时间】: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<SelectListItems> Location(当您有视图模型时,切勿使用ViewBag)
标签: asp.net-mvc asp.net-core-mvc viewmodel selectlistitem