无法隐式转换MVC中的类型问题

时间:2023-01-15 16:31:10

Hi I am fairly new to MVC.

嗨,我对MVC很新。

So I have a model defined as

所以我将模型定义为

[Table("Roles")]
public class RolesModel
{
    [Key]
    [Display(Name = "Role ID")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int RoleID { get; set; }

    [Display(Name = "Role Name")]
    public string RoleName { get; set; }
}

And in the controller, I want to create a getall class doing like:

在控制器中,我想创建一个getall类,如:

        public List<RolesController> GetAll()
    {
        return db.RolesModels.ToList();
    }

However, It keeps giving me the error for Cannot implicitly convert type issues

但是,它不断给我错误,不能隐式转换类型问题

Did I miss anything here please.

我在这里错过了什么吗

Thank you

1 个解决方案

#1


1  

The error message should indicate which types it is trying to convert that it can't. In GetALL() the return is returning a list of RolesModels but the method signature indicates it should return a list of RolesControllers. It does not know how to convert between the two. You have to provide that but you might just have an incorrect signature.

错误消息应指出它尝试转换的类型,但不能。在GetALL()中,返回返回RolesModel列表,但方法签名表明它应返回RolesControllers列表。它不知道如何在两者之间进行转换。你必须提供,但你可能只有一个不正确的签名。

#1


1  

The error message should indicate which types it is trying to convert that it can't. In GetALL() the return is returning a list of RolesModels but the method signature indicates it should return a list of RolesControllers. It does not know how to convert between the two. You have to provide that but you might just have an incorrect signature.

错误消息应指出它尝试转换的类型,但不能。在GetALL()中,返回返回RolesModel列表,但方法签名表明它应返回RolesControllers列表。它不知道如何在两者之间进行转换。你必须提供,但你可能只有一个不正确的签名。