控制器错误“Microsoft.AspNetCore.Mvc.Rendering.

我想在 MVC 中创建一个 SelectList 下拉列表。


我更喜欢选择列表在存储库中,而不是在控制器中。我如何调用存储库,甚至不引用模型中字段的名称。我唯一想提到的是存储库。


我收到此“Microsoft.AspNetCore.Mvc.Rendering.SelectListItem”的错误 此资源还没有帮助: 为什么我在 DropDownList 中收到“System.Web.Mvc.SelectListItem”?


我在网上阅读了很多例子,这是原始代码,


原始代码:


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>


犯罪嫌疑人X
浏览 203回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP