未为新模型值分配OnPost

时间:2019-04-30 13:30:10

标签: c# asp.net-core-mvc entity-framework-core razor-pages asp.net-core-2.2

上传文件并提取FileName之后,在更新数据库之前,无法将值分配给我的模型。文件已正确上传,表格中的其他数据已更新,并且没有错误发生。

表格:

<form method="post" enctype="multipart/form-data">
    <input asp-for="Product.Name" />
    <input type="file" name="ImageFile" />
    @for(var i = 0; i < 10; i++) {
        <input asp-for="Product.ProductIntervals[i].Name" />
        [...]
    }
</form>

OnPostAsync:

public async Task<IActionResult> OnPostAsync(CreateEditProduct model, IFormFile imageFile)
{

    if (!ModelState.IsValid) return Page();

    if (imageFile != null)
    {
        var fileName = imageFile.FileName;
        var uploadPath = Path.Combine(_hostingEnvironment.WebRootPath, "uploads");
        imageFile.CopyTo(new FileStream(uploadPath, FileMode.Create));
        model.Product.Image = fileName;
    }

    if (await TryUpdateModelAsync(
        model.Product,
        "Product",
        x => x.Name,
        //x => x.Image
    ))
    {

        _dbContext.Update(model.Product);

        foreach (var x in model.Product.ProductIntervals)
        {
            bool newInterval = x.Id == Guid.Empty;

            var interval = new ProductInterval
            {
                Id = newInterval ? Guid.NewGuid() : x.Id,
                Name = x.Name,
                IntervalMonths = x.IntervalMonths,
                IntervalDays = x.IntervalDays,
                Priority = x.Priority,
                ProductId = model.Product.Id,
                Status = x.Status
            };

            if (newInterval)
            {
                await _dbContext.AddAsync(interval);
            }
            else
            {
                _dbContext.Update(interval);
            }
        }

        await _dbContext.SaveChangesAsync();

        return RedirectToPage("./index");

    }

    return Page();

}

模型:

public class Product
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Image { get; set; }
    public IList<ProductInterval> ProductIntervals { get; set; }
}

public class CreateEditProduct {

    public Product Product { get; set; }
    public IList<ProductCategory> ProductCategories { get; set; }

}

我尝试了TryUpdateModelAsync中是否有x => x.Image,没有区别。

怎么了?

1 个答案:

答案 0 :(得分:0)

我认为您缺少模型和DbContext之间的链接-除非您在简化的代码部分中指定了链接。

例如将_dbContext.Update()调用添加到您的控制器方法可以解决此问题?

public async Task<IActionResult> OnPostAsync(CreateEditProduct model, IFormFile imageFile, string existingImage)
{

    if (!ModelState.IsValid) return Page();

    if (imageFile != null)
    {
        var fileName = imageFile.FileName;
        [...]
        model.Product.Image = fileName;
    }

    if (await TryUpdateModelAsync(
        model.Product,
        "Product",
        x => x.Image,
        [...]
    )){
        _dbContext.Update(model); // or model.Product if you only need to save changes to Product
        await _dbContext.SaveChangesAsync();
    }

或者,您可以在验证检查之后立即调用_dbContext.attach(model);,以使Entity Framework跟踪模型的相关更改。