C# 未在POST上指定新模型值
上传文件并提取文件名后,在更新数据库之前,我无法将该值分配给我的模型。文件已正确上载,表单中的其他数据已更新,且未发生任何错误 表格:C# 未在POST上指定新模型值,c#,asp.net-core-mvc,entity-framework-core,razor-pages,asp.net-core-2.2,C#,Asp.net Core Mvc,Entity Framework Core,Razor Pages,Asp.net Core 2.2,上传文件并提取文件名后,在更新数据库之前,我无法将该值分配给我的模型。文件已正确上载,表单中的其他数据已更新,且未发生任何错误 表格: <form method="post" enctype="multipart/form-data"> <input asp-for="Product.Name" /> <input type="file" name="ImageFile" /> @for(var i = 0; i < 10; i+
<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>
@对于(变量i=0;i<10;i++){
[...]
}
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();
}
PostAsync上的公共异步任务(CreateEditProduct模型,IFormFile imageFile)
{
如果(!ModelState.IsValid)返回页面();
if(imageFile!=null)
{
var fileName=imageFile.fileName;
var uploadPath=Path.Combine(_hostingEnvironment.WebRootPath,“uploads”);
CopyTo(新文件流(uploadPath,FileMode.Create));
model.Product.Image=文件名;
}
如果(等待TryUpdateModelAsync(
型号,产品,,
“产品”,
x=>x.Name,
//x=>x.图像
))
{
_dbContext.Update(model.Product);
foreach(model.Product.ProductInterval中的var x)
{
bool newInterval=x.Id==Guid.Empty;
var间隔=新产品间隔
{
Id=newInterval?Guid.NewGuid():x.Id,
Name=x.Name,
IntervalMonths=x.IntervalMonths,
间隔天数=x.间隔天数,
优先级=x.优先级,
ProductId=model.Product.Id,
状态=x.状态
};
如果(新间隔)
{
wait_dbContext.AddAsync(interval);
}
其他的
{
_dbContext.Update(间隔);
}
}
wait_dbContext.saveChangesSync();
返回页首(“/索引”);
}
返回页();
}
模型:
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; }
}
公共类产品
{
公共Guid Id{get;set;}
公共字符串名称{get;set;}
公共字符串图像{get;set;}
公共IList ProductInterval{get;set;}
}
公共类CreateEditProduct{
公共产品产品{get;set;}
公共IList ProductCategories{get;set;}
}
我在tryupdatemodelsync中尝试了使用/不使用x=>x.Image,没有任何区别
怎么了?我认为您缺少了模型和
DbContext
之间的链接,除非您在简化后的代码部分的某个地方指定了它
例如,向控制器方法添加\u 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();
}
PostAsync上的公共异步任务(CreateEditProduct模型、IFormFile imageFile、字符串existingImage)
{
如果(!ModelState.IsValid)返回页面();
if(imageFile!=null)
{
var fileName=imageFile.fileName;
[...]
model.Product.Image=文件名;
}
如果(等待TryUpdateModelAsync(
型号,产品,,
“产品”,
x=>x.图像,
[...]
)){
_dbContext.Update(model);//或model.Product,如果您只需要保存对产品的更改
wait_dbContext.saveChangesSync();
}
或者,您可以在验证检查之后立即调用
\u dbContext.attach(model);
,让实体框架跟踪对模型的相关更改。我认为您缺少模型和dbContext
之间的链接,除非您在简化后的代码部分的某个地方指定了它
例如,向控制器方法添加\u 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();
}
PostAsync上的公共异步任务(CreateEditProduct模型、IFormFile imageFile、字符串existingImage)
{
如果(!ModelState.IsValid)返回页面();
if(imageFile!=null)
{
var fileName=imageFile.fileName;
[...]
model.Product.Image=文件名;
}
如果(等待TryUpdateModelAsync(
型号,产品,,
“产品”,
x=>x.图像,
[...]
)){
_dbContext.Update(model);//或model.Product,如果您只需要保存对产品的更改
wait_dbContext.saveChangesSync();
}
或者,您可以调用
\u dbContext.attach(model);
验证后立即进行检查,让实体框架跟踪对模型的相关更改。我认为您希望在模型中包含该文件。您可以发布模型吗class@ThamaraiT请看更新后的问题。@Jonesopolis通常我就是这样做的,它应该会起作用。我想你会想包括你能把你的模型贴出来吗class@ThamaraiT请查看更新后的问题。@Jonesopolis通常我就是这样做的,它应该会起作用。你可能是对的。我没有这样做,但其他时候我没有。我用更多的数据更新了我的问题,因为当我插入\u dbContext.Update时(model.Product);
我在更新时间间隔时出错,表示实体已被跟踪。如果我这样做了,会有什么区别吗var Product=new Product{Name=model.Product.Name,Image=imageFile};
而不是tryupdatemodelsync
?更新间隔时出现了什么具体错误?invalidoOperationException:无法跟踪实体类型“ProductInterval”的实例,因为另一个实例具有{Id'的相同键值已经被跟踪。
我会更新我的答案,因为注释的代码太多了,你可能是对的。我没有,但其他时候我没有。我用更多的数据更新了我的问题,因为当我插入\u dbContext.update(model.Product)时
我在更新间隔时收到一个错误,表示实体已被跟踪。如果我这样做了,var-product=new-product{Name=model.product.Name,Image=imageFile};
而不是tryupdatemodelsync
,会有什么区别吗?现在更新interva时会出现什么具体错误