Posting entity class when only some fields are provided by user(当用户仅提供某些字段时过帐实体类)
问题描述
我很好奇人们是如何处理这件事的。
我已使用Visual Studio将具有实体框架的CRUD页添加到我的Razor Pages应用程序中。
但是,事实上,创建页面是不可能进行验证的。这是因为实体具有诸如用户ID、具有缺省值的创建日期等字段,以及我想要设置缺省值的几个字段。所以ModelState.IsValid
显然会返回false
。而且,用户为实体的每个字段提供完全正确的数据的情况似乎很少发生。
处理此问题的一种方法是创建一个仅包含我需要的用户字段的新类,然后在将其保存到数据库之前将其复制到实体。
另一种选择是编写代码,在调用ModelState.IsValid
之前填充不依赖于用户输入的字段。
这对我来说是一个新情况。我很想听听其他人是如何处理这种情况的。
推荐答案
在我看来,如果您可以使用AutoMapper
而不是复制值,您的第一个选项会更简单、更安全。
请参阅以下使用ASP.NET core 3.0 Razor的演示页面:
1.Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection
2.创建模型(保存到数据库)和查看模型(在用户页面上显示)
public class Movie
{
public Movie()
{
Genre = "Action";
}
public int ID { get; set; }
public string Title { get; set; }
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
}
public class MovieViewModel
{
public string Title { get; set; }
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
}
3.创建自动映射器Profile
类
public class MovieProfile: Profile
{
public MovieProfile()
{
CreateMap<MovieViewModel, Movie>().ReverseMap();
}
}
4.Create.cshtml.cs
public class CreateModel : PageModel
{
private readonly ApplicationContext _context;
private readonly IMapper _mapper;
public CreateModel(ApplicationContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public IActionResult OnGet()
{
return Page();
}
[BindProperty]
public MovieViewModel MovieVM { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
Movie movie = _mapper.Map<Movie>(MovieVM);
_context.Movie.Add(movie);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
}
Create.cshtml
@page
@model RazorpagesCore.Pages.Movies.CreateModel
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="MovieVM.Title" class="control-label"></label>
<input asp-for="MovieVM.Title" class="form-control" />
<span asp-validation-for="MovieVM.Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="MovieVM.ReleaseDate" class="control-label"></label>
<input asp-for="MovieVM.ReleaseDate" class="form-control" />
<span asp-validation-for="MovieVM.ReleaseDate" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
这篇关于当用户仅提供某些字段时过帐实体类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当用户仅提供某些字段时过帐实体类


- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01