C# 不实现接口成员MVC
我无法使后期动作发挥作用。它给我的错误是:IAuthorService in Services: 下面是我的代码: “AuthorService”未实现intreface成员“IAuthorService.Post(AutthorViewModel)”。“IAuthorService.Post(AutthorViewModel)”无法实现“IAuthorService.Post(AutthorViewModel)”,因为它没有匹配的reurn类型“AuthorViewModel” 作者类别:C# 不实现接口成员MVC,c#,asp.net-mvc,C#,Asp.net Mvc,我无法使后期动作发挥作用。它给我的错误是:IAuthorService in Services: 下面是我的代码: “AuthorService”未实现intreface成员“IAuthorService.Post(AutthorViewModel)”。“IAuthorService.Post(AutthorViewModel)”无法实现“IAuthorService.Post(AutthorViewModel)”,因为它没有匹配的reurn类型“AuthorViewModel” 作者类别: n
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; }
}
}
名称空间实体
{
公共类作者
{
公共作者()
{
书籍=新列表();
}
公共int ID{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共列表书籍{get;set;}
}
}
接口:
namespace Interfaces.RepositoryIntefaces
{
public interface IAuthorRepository
{
List<Author> GetAllFromRepo();
void PostFromRepo(Author author);
}
}
namespace Interfaces.RepositoryIntefaces
{
公共接口IAuthorRepository
{
列出GetAllFromRepo();
无效PostFromRepo(作者);
}
}
存储库:
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();
}
}
}
命名空间存储库
{
公共类AuthorRepository:IAuthorRepository
{
AppContext myDB=新的AppContext();
公共列表GetAllFromRepo()
{
返回myDB.Authors.Include(a=>a.Books.ToList();
}
公开作废PostFromRepo(作者)
{
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>()
});
}
}
}
命名空间服务
{
公共类AuthorService:IAuthorService//错误如下
{
私人IAuthorRepository\u AuthorRepository;
公共作者服务(IAuthorRepository authorRepository)
{
_AuthorRepository=AuthorRepository;
}
公共列表GetAll()
{
列出作者=_AuthorRepository.GetAllFromRepo();
返回authors.Select(x=>newauthorViewModel()
{
ID=x.ID,
FirstName=x.FirstName,
LastName=x.LastName,
Books=x.Books.Select(g=>newbookviewmodel()
{
ID=g.ID,
Name=g.Name
})托利斯先生()
}).ToList();
}
公开作废帖子(AuthorViewModel作者)
{
_AuthorRepository.PostFromRepo(新作者()
{
FirstName=author.FirstName,
LastName=author.LastName,
图书=新列表()
});
}
}
}
作者视图模型:
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; }
}
}
名称空间视图模型
{
公共类AuthorViewModel
{
公共AuthorViewModel()
{
书籍=新列表();
}
公共int ID{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共列表书籍{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();
}
}
}
名称空间WebApp.Controllers
{
公共类HomeController:控制器
{
私有只读IAAuthorService(U AuthorService);
公共家庭控制器(IAuthorService authorService)
{
_AuthorService=AuthorService;
}
公共行动结果索引()
{
列出作者=_AuthorService.GetAll();
返回视图(Authors.ToList());
}
[HttpPost]
公共操作结果创建()
{
_AuthorService.Post(新的AuthorViewModel(){FirstName=“Olivia”,LastName=“Fox”,Books=new List()});
返回视图();
}
}
}
这是我在VS2017的错误列表中得到的错误:
严重性代码说明项目文件行抑制状态
错误CS0738“AuthorService”未实现接口成员“IAAuthorService.Post(AuthorViewModel)”AuthorService.Post(AuthorViewModel)“无法实现”iaAuthorService.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);
}
}
namespace Interfaces.ServiceInterfaces
{
公共接口IAuthorService
{
List GetAll();
AuthorViewModel Post(AuthorViewModel作者);
}
}
我认为这是直截了当的。您的接口的返回类型为AuthorViewModel
,而您的类的返回类型为void。无法实现。。。因为它没有匹配的返回类型…
表示方法的返回类型在接口和类实现之间不匹配。检查该方法是否正确实施。请发布IAuthorService
的代码?界面IAuthorService
methodsignature和AuthorService
类似乎是不同的。我发布了它。很抱歉,开始时忘了发布。错误的意思正是它所说的。您的界面在Post
上有一个返回类型。您的实现没有。签名不同void
vsAuthorViewModel
在IAuthorService中将AuthorViewModel更改为void删除了错误。创建控制器现在似乎不起作用,但我想这是另一个问题。