当用户仅提供某些字段时过帐实体类 [英] Posting entity class when only some fields are provided by user

查看:36
本文介绍了当用户仅提供某些字段时过帐实体类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇人们是如何处理这件事的。

我已使用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");}
}

这篇关于当用户仅提供某些字段时过帐实体类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆