新模型值未分配 OnPost
New model value is not assigned OnPost
上传文件并提取文件名后,我无法在更新数据库之前将值分配给我的模型。文件已正确上传,表格中的其他数据已更新且未出现错误。
形式:
<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 中尝试了 with/without x => x.Image,没有任何区别。
怎么了?
我认为您在模型和 DbContext
之间遗漏了 link - 除非您在简化的代码部分的某处指定了它。
例如将 _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 跟踪模型的相关更改。
上传文件并提取文件名后,我无法在更新数据库之前将值分配给我的模型。文件已正确上传,表格中的其他数据已更新且未出现错误。
形式:
<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 中尝试了 with/without x => x.Image,没有任何区别。
怎么了?
我认为您在模型和 DbContext
之间遗漏了 link - 除非您在简化的代码部分的某处指定了它。
例如将 _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 跟踪模型的相关更改。