【问题标题】:are you missing a cast?你错过了演员表吗?
【发布时间】:2017-02-26 10:24:36
【问题描述】:

无法将类型“DoctorFront.Models.DocMaster”隐式转换为“System.Collections.IEnumerable”。存在显式转换(您是否缺少演员表?)

namespace DoctorFront.Controllers
{
    public class DoctorController : ApiController
    {
        static readonly IDocRepositories Repository  =new DocRepositories();
        public IEnumerable GetAllDoctor()
        {
            return Repository.GetAll();
        }

        public IEnumerable GetDoctor(int id) 
        {

            return Repository.Get(id);
        }

    }
}

【问题讨论】:

  • 我猜你的GetDoctor 方法是为了返回DocMaster,对吧?
  • 将返回类型更改为DocMaster
  • 是的,它返回文档主表
  • 谢谢@Amit Kumar Ghosh 它成功了

标签: .net razor asp.net-mvc-5


【解决方案1】:
public IEnumerable GetDoctor(int id) 
{

    return Repository.Get(id); // problem here
}

您的方法GetDoctor 应该返回一个IEnumerable 类型的对象。你的 return 语句只返回一个 single DocMaster 对象。

所以这应该有效:

public DocMaster GetDoctor(int id) 
{

    return Repository.Get(id);
}

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    正如您需要对任何方法执行的操作,您的返回值必须与方法声明相匹配。所以如果你有一个方法public IEnumerable Get(int id),你需要返回一个实现IEnumerable的任何类。

    BviLLe_Kid 给出的示例显示了正确更正以停止接收此错误。

    这是一个更完整的例子:

    public IHttpActionResult Get(int id) 
    {
        var doc = Repository.Get(id);
    
        if (doc == null)
            return NotFound();
    
        return Ok(doc);
    }
    

    如果您正在使用Web API,最好阅读一下rest

    【讨论】:

      猜你喜欢
      • 2017-08-08
      • 2015-12-23
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 2015-06-24
      • 2020-05-18
      • 2019-05-10
      • 1970-01-01
      相关资源
      最近更新 更多