【发布时间】:2018-05-24 21:27:44
【问题描述】:
我想在 MVC 中创建一个 SelectList Dropdown。
我更喜欢选择列表在存储库中,而不是在控制器中。 我如何调用存储库,甚至不参考模型中的字段名称。我只想参考存储库。
我收到此“Microsoft.AspNetCore.Mvc.Rendering.SelectListItem”的错误 此资源还没有帮助: Why I am getting "System.Web.Mvc.SelectListItem" in my DropDownList?
我在网上阅读了很多例子,这是原始代码,
原始代码:
ViewData["ProductTypeId"] = new SelectList(_context.ProductType, "ProductName", "ProductDescription");
我正在尝试让代码像这样工作:
接收错误:“Microsoft.AspNetCore.Mvc.Rendering.SelectListItem”
ViewData["ProductTypeId"] = new SelectList(_producttyperepository.GetAllLookupKey());
public IEnumerable<SelectListItem> GetAllLookupKey()
{
return (from ptin _context.ProductType pt
select new SelectListItem
{
Value = pt.ProductTypeName,
Text = pt.ProductDescription
});
}
<div class="form-group">
<label asp-for="ProductType" class="control-label"></label>
<select asp-for="ProductType" class="form-control" asp-items="ViewBag.ProductTypeId"></select>
<span asp-validation-for="ProductType" class="text-danger"></span>
</div>
【问题讨论】:
-
嗯,试试
ViewData["ProductTypeId"] = _producttyperepository.GetAllLookupKey(); -
命名令人困惑。 POCO 实体命名为
ProductType。视图模型是否有属性名称ProductType? -
已尝试“在编译处理此请求所需的资源期间发生错误。请查看以下具体错误详细信息并适当修改您的源代码。”它最初与 Viewbag 合作,出于某种原因,将重新审视
-
是的,Win正确,viewmodel也是产品类型,我要重命名这个,3个月前开始编程
标签: c# asp.net-mvc asp.net-core