【发布时间】:2018-07-07 22:13:51
【问题描述】:
在学习了一点 ASP.NET Core MVC 之后,我构建了一个简单的基于 CRUD 的 Web 应用程序。虽然很简单,但我现在正在尝试学习如何通过将业务逻辑和控制器外部的数据访问移动到它们自己的层来制作更小的控制器。问题是控制器中的模型绑定不容易传递给其他层。
在将所有业务和数据访问逻辑移到控制器之外之前,实体的创建工作已经完成。还有一个 GetAll() 方法(下面未显示),它使用单独的层来获取和显示存储在数据库中的所有实体。但是在尝试创建实体时(见下图),出现以下错误:InvalidOperationException: Could not create an instance of type 'MyProject.Models.Entity'. Model bound complex types must not be abstract or value types and must have a parameterless constructor.
如何将由 View 创建的模型绑定对象传递到 Web 应用程序的 n 层?
Entity.cs
namespace MyProject.Models
{
public enum EntityType { Type1, Type2 }
public class Entity
{
private readonly ApplicationDbContext context;
public Entity(ApplicationDbContext _context)
{
context = _context;
}
public int EntityId { get; set; }
public string Name { get; set; }
public DateTime Date1 { get; set; }
public EntityType Type { get; set; }
}
}
CreateEntityModel.cs
namespace MyProject.Models
{
public class CreateEntityModel
{
public string Name { get; set; }
public string Date1 { get; set; }
public int Type { get; set; }
}
}
创建.cshtml
@model CreateEntityModel
<h1>Create Entity</h1>
<form asp-controller="entity" asp-action="createentity" method="post">
<div class="form-group">
<label asp-for="Name"></label>
<input asp-for="Name" class="form-control" />
</div>
<div class="form-group">
<label asp-for="Date1"></label>
<input asp-for="Date1" class="form-control" />
</div>
<div class="form-group">
<label asp-for="Type"></label>
<select asp-for="Type">
<option selected>Choose entity type</option>
<option value="0">Type1</option>
<option value="1">Type2</option>
</select>
</div>
<button type="submit">Create</button>
</form>
EntityController.cs
namespace MyProject.Controllers
{
public class EntityController : Controller
{
private UserManager<ApplicationUser> userManager;
private readonly ApplicationDbContext context;
private EntityService entityService;
public EntityController(UserManager<ApplicationUser> _userManager,
ApplicationDbContext _context)
{
userManager = _userManager;
context = _context;
entityService= new EntityService (_userManager, _context);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CreateEntity([Bind("Name, Date1, Type")] Entity entity)
{
if (ModelState.IsValid)
{
await entityService.Create(entity);
return RedirectToAction(nameof(Index));
}
return View("~/Views/MyView");
}
}
}
EntityService.cs
namespace MyProject.Business
{
public class EntityService
{
private UserManager<ApplicationUser> userManager;
private readonly ApplicationDbContext context;
private EntityRepository entityRepository;
public EntityService(
UserManager<ApplicationUser> _userManager,
ApplicationDbContext _context)
{
userManager = _userManager;
context = _context;
entityRepository = new EntityRepository(_context);
}
public async Task Create(Entity entity)
{
await entityRepository.Create(entity);
}
}
}
EntityRepository.cs
namespace MyProject.Data
{
public class EntityRepository
{
private readonly ApplicationDbContext context;
public EntityRepository(ApplicationDbContext _context)
{
context = _context;
}
public async Task Create(Entity entity)
{
context.Add(entity);
await context.SaveChangesAsync();
}
}
}
【问题讨论】:
标签: c# asp.net-core entity-framework-core