c# - 不实现接口成员 MVC
问题描述
我无法使发布操作起作用。它给了我错误:服务中的IAuthorService:这是我下面的代码:
“AuthorService”没有实现接口成员“IAuthorService.Post(AuthorViewModel)”。“IAuthorService.Post(AuthorViewModel)”无法实现“IAuthorService.Post(AuthorViewModel)”,因为它没有匹配的“AuthorViewModel”重复类型。
作者等级:
namespace Entities
{
public class Author
{
public Author()
{
Books = new List<Book>();
}
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Book> Books { get; set; }
}
}
界面:
namespace Interfaces.RepositoryIntefaces
{
public interface IAuthorRepository
{
List<Author> GetAllFromRepo();
void PostFromRepo(Author author);
}
}
存储库:
namespace Repositories
{
public class AuthorRepository : IAuthorRepository
{
AppContext myDB = new AppContext();
public List<Author> GetAllFromRepo()
{
return myDB.Authors.Include(a=> a.Books).ToList();
}
public void PostFromRepo(Author author)
{
myDB.Authors.Add(author);
myDB.SaveChanges();
}
}
}
服务:
namespace Services
{
public class AuthorService : IAuthorService // here is the error
{
private IAuthorRepository _AuthorRepository;
public AuthorService(IAuthorRepository authorRepository)
{
_AuthorRepository = authorRepository;
}
public List<AuthorViewModel> GetAll()
{
List<Author> authors = _AuthorRepository.GetAllFromRepo();
return authors.Select(x => new AuthorViewModel()
{
ID = x.ID,
FirstName = x.FirstName,
LastName = x.LastName,
Books = x.Books.Select(g => new BookViewModel()
{
ID = g.ID,
Name = g.Name
}).ToList()
}).ToList();
}
public void Post(AuthorViewModel author)
{
_AuthorRepository.PostFromRepo(new Author()
{
FirstName = author.FirstName,
LastName = author.LastName,
Books = new List<Book>()
});
}
}
}
作者视图模型:
namespace ViewModels
{
public class AuthorViewModel
{
public AuthorViewModel()
{
Books = new List<BookViewModel>();
}
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<BookViewModel> Books { get; set; }
}
}
控制器:
namespace WebApp.Controllers
{
public class HomeController : Controller
{
private readonly IAuthorService _AuthorService;
public HomeController(IAuthorService authorService)
{
_AuthorService = authorService;
}
public ActionResult Index()
{
List<AuthorViewModel> Authors = _AuthorService.GetAll();
return View(Authors.ToList());
}
[HttpPost]
public ActionResult Create()
{
_AuthorService.Post(new AuthorViewModel() { FirstName = "Olivia", LastName = "Fox", Books = new List<BookViewModel>() });
return View();
}
}
}
这是我在 VS2017 的错误列表中得到的错误:
严重性代码说明项目文件行抑制状态错误 CS0738“AuthorService”未实现接口成员“IAuthorService.Post(AuthorViewModel)”。“AuthorService.Post(AuthorViewModel)”无法实现“IAuthorService.Post(AuthorViewModel)”,因为它没有匹配的返回类型“AuthorViewModel”。服务 C:\Users\Jack\Desktop\WebApp Domasna 1.9\Services\AuthorService.cs 14 活动
IAuthorService 的代码:
namespace Interfaces.ServiceInterfaces
{
public interface IAuthorService
{
List<AuthorViewModel> GetAll();
AuthorViewModel Post(AuthorViewModel author);
}
}
解决方案
我认为这是直截了当的。您的接口的返回类型为 , AuthorViewModel
而您的类的返回类型为 void。
推荐阅读
- python-3.x - 获取 Pandas 系列中拆分字符串列表的第一项
- c - 为什么我的 hello-1.c 和 Makefile 没有生成 .ko 文件?
- django - 如何解决 SSL SYSCALL 错误:芹菜文件描述符错误?
- flutter - 如何将 RefreshIndicator 添加到列表的子部分?
- rust - 检查 Rust 中路径组件的权限
- php - 为什么我的 if else 语句有效,但是当我用函数替换它时却不起作用?
- excel - 如何计算第一个填充的合并单元格?
- mysql - SQL将行乘以数组中的每个元素
- heroku - 我可以重复使用旧的(已删除的)heroku 应用名称吗?
- python - 基于 CNN 模型预测值的车牌裁剪图像