当我尝试执行Sofdelete时出现错误
不能隐式转换类型System.Collections.Generic.List<# #。# # .Employee> '"Microsoft.AspNetCore.Mvc.IActionResult"。
这是我的索引,我试图使用ToList()
和ToList<Employee>
,但它不工作
public IActionResult Index()
{
var employees = _dbContext.Employees.Where(x => x.status == '1')
.ToList();
return employees;
}
MyDbContext
:
public class DataContext : DbContext
{
public DataContext(DbContextOptions options) : base(options)
{
}
public DbSet<Employee> Employees { get; set; }
public override int SaveChanges()
{
foreach( var entry in ChangeTracker.Entries())
{
var entity = entry.Entity;
if (entry.State == EntityState.Deleted)
{
entry.State = EntityState.Modified;
entity.GetType().GetProperty("status").SetValue(entity, '0');
}
}
return base.SaveChanges();
}
}
员工:
namespace empAPI.Models
{
public class Employee
{
public Guid Id { get; set; }
public char status{ get; set; } = '1';
public string Name { get; set; }
public string Department { get; set; }
public DateTime? CreatedDate { get; set; } = DateTime.Now;
}
}
修改代码为:
public IActionResult Index()
{
var employees = _dbContext.Employees.Where(x => x.status == '1').ToList();
return View(employees);
}
阅读以下文章:理解行动结果
控制器操作返回称为操作结果的东西。一个动作结果是控制器动作在响应浏览器请求。
ASP。. NET MVC框架支持几种类型的操作结果包括:
- ViewResult -表示HTML和标记。
- empty -表示没有结果。
- redirecresult -表示重定向到新的URL。
- JsonResult -表示可以在AJAX应用中使用的JavaScript对象表示法结果。
- JavaScriptResult -表示一个JavaScript脚本。
- ContentResult -表示文本结果。
- FileContentResult -表示可下载文件(包含二进制内容)。
- FilePathResult -表示一个可下载的文件(带路径)。
- FileStreamResult -表示可下载文件(带有文件流)。
所有这些操作结果都继承自ActionResult基类。
在大多数情况下,控制器动作返回ViewResult。