首页 > 解决方案 > 不实现接口成员 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);
    }
}

标签: c#asp.net-mvc

解决方案


我认为这是直截了当的。您的接口的返回类型为 , AuthorViewModel而您的类的返回类型为 void。


推荐阅读