控制器错误中的MVC核心SelectList下拉菜单"Microsoft.AspNetCore.Mvc.Rendering.SelectListItem" [英] MVC Core SelectList Dropdown in Controller Error "Microsoft.AspNetCore.Mvc.Rendering.SelectListItem"

查看:273
本文介绍了控制器错误中的MVC核心SelectList下拉菜单"Microsoft.AspNetCore.Mvc.Rendering.SelectListItem"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create a SelectList Dropdown in MVC.

我希望选择列表位于存储库中,而不是在控制器中. 如何调用存储库,甚至不引用模型中字段的名称.我唯一要参考的是存储库.

I prefer select list to be in repository, not in controller. How do I call a repository, without even referring to the name of the fields in the Model. Only thing I want to refer to is repository.

我收到与此"Microsoft.AspNetCore.Mvc.Rendering.SelectListItem"有关的错误 此资源尚无帮助: 为什么我正在获取"System.Web.Mvc.SelectListItem";在我的DropDownList中?

I am receiving error with this "Microsoft.AspNetCore.Mvc.Rendering.SelectListItem" This resource does not help yet: Why I am getting "System.Web.Mvc.SelectListItem" in my DropDownList?

我正在互联网上阅读许多示例,这是原始代码,

I am reading many examples on internet, This is the original code,

原始代码:

ViewData["ProductTypeId"] = new SelectList(_context.ProductType, "ProductName", "ProductDescription");

我正在尝试使代码像这样工作:

收到错误:"Microsoft.AspNetCore.Mvc.Rendering.SelectListItem"

Receive error: "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>

推荐答案

您当前的GetAllLookupKey无法编译!使用此版本.

Your current GetAllLookupKey would not compile! Use this version.

public IEnumerable<SelectListItem> GetAllLookupKey()
{
    return _context.ProductType.Select( pt=>new SelectListItem
    {
        Value = pt.ProductTypeName,
        Text = pt.ProductDescription
    }).ToList();
}

此外,也无需创建第二个SelectList.您的GetAllLookupKey方法返回SelectListItem的集合,您可以将其设置为ViewData.

Also, there is no need to create a second SelectList. Your GetAllLookupKey method returns a collection of SelectListItem and you can set that to your ViewData.

 ViewData["ProductTypeId"] = _producttyperepository.GetAllLookupKey();

SelectListItem类是在名称空间Microsoft.AspNetCore.Mvc.Rendering;中定义的,因此请确保您具有在类中包含该名称空间的using语句.

The SelectListItem class is defined in the namespace Microsoft.AspNetCore.Mvc.Rendering; So make sure you have a using statement which includes that namespace in your class.

using Microsoft.AspNetCore.Mvc.Rendering;

如果该项目与具有Controllers的项目不在同一个项目中.确保您的项目引用了具有此名称空间&的程序集.课.

If this is in a different project than the one which has the Controllers. make sure your project has a reference to the assembly which has this namespace & class.

虽然可能会解决我们的问题,但SelectListItem是一个代表更多UI层问题的类. IMHO ,您不应让存储库返回该信息.让您的存储库返回更多通用数据(例如:ProductType列表),并让您的UI层代码(控制器操作/UI服务层等)从此ProductType列表中生成SelectListItem对象的列表.

While that might fix our problem, SelectListItem is a class to represent more of a UI layer concern. IMHO, You should not have your repository return that. Have your repository return more generic data (Ex : List of ProductType) and have your UI layer code (controller action/UI service layer etc) generate the list of SelectListItem objects from this ProductType list.

这篇关于控制器错误中的MVC核心SelectList下拉菜单"Microsoft.AspNetCore.Mvc.Rendering.SelectListItem"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆