【发布时间】:2021-04-23 00:19:45
【问题描述】:
我是 ASP.NET Core 3.1 的初学者,我正在使用 Microsoft 网站上的 EF Core 教程创建此应用程序,当我尝试创建新作业并存储照片时,照片属性在数据库上显示为空,我没有知道为什么? 我使用调试器查看图像是否进入控制器中的 Create 方法,并且 IFormFile Image 参数说它为空。
型号:
public int JobID { get; set; }
[Required]
[Column("Title")]
[StringLength(150, MinimumLength = 3)]
[Display(Name = "Title")]
public string Title { get; set; }
[Display(Name = "Photo")]
public byte[] Photo { get; set; }
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Date")]
public DateTime Date { get; set; }
[Required]
[Column("CategoryID")]
[Display(Name = "CategoryID")]
public int CategoryID { get; set; }
[Required]
[Column("CompanyID")]
[Display(Name = "CompanyID")]
public int CompanyID { get; set; }
public Category Category { get; set; }
public Company Company { get; set; }
public ICollection<Application> Applications { get; set; }
查看 - Create.html:
<form asp-action="Create" method="post" enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Title" class="control-label"></label>
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Photo" class="control-label"></label>
<input type="file" name="Photo" class="form-control" />
@*<span asp-validation-for="Photo" class="text-danger"></span>*@
</div>
<div class="form-group">
<label asp-for="Date" class="control-label"></label>
<input asp-for="Date" class="form-control" />
<span asp-validation-for="Date" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="CategoryID" class="control-label"></label>
<select asp-for="CategoryID" class ="form-control" asp-items="ViewBag.CategoryID"></select>
</div>
<div class="form-group">
<label asp-for="CompanyID" class="control-label"></label>
<select asp-for="CompanyID" class ="form-control" asp-items="ViewBag.CompanyID"></select>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
控制器方法 JobsController:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("JobID,Title,Photo,Date,CategoryID,CompanyID")] Job job, IFormFile Image)
{
if (ModelState.IsValid)
{
if (Image != null) {
if (Image.Length > 0) {
byte[] p1 = null;
using (var fs1 = Image.OpenReadStream())
using (var ms1 = new MemoryStream())
{
fs1.CopyTo(ms1);
p1 = ms1.ToArray();
}
job.Photo = p1;
}
}
_context.Add(job);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["CategoryID"] = new SelectList(_context.Categories, "CategoryID", "CategoryID", job.CategoryID);
ViewData["CompanyID"] = new SelectList(_context.Companies, "CompanyID", "Name", job.CompanyID);
return View(job);
}
这里是 null:IFormFile Image null, Job Photo 属性也为 null
public async Task<IActionResult> Create([Bind("JobID,Title,Photo,Date,CategoryID,CompanyID")] Job job, IFormFile Image)
其他属性存储良好,我尝试了几乎所有类似的解决方案,但没有一个对我有帮助。有什么想法吗?
【问题讨论】:
标签: asp.net asp.net-mvc asp.net-core asp.net-core-3.1