我刚刚尝试了很多我发现的东西,但最后都没有成功。
首先,我有下一个代码,它只是做的事情很好,但不保存图像。
我应该怎么做才能将图像保存到 varbinarymax 中?以及接下来如何将它们展示给视图?
看法:
<div class="form-group">
@Html.LabelFor(model => model.Logo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.Logo, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.TextBoxFor(model => model.Logo, new { type = "file" })
@Html.ValidationMessageFor(model => model.Logo, "", new { @class = "text-danger" })
</div>
</div>
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,Name,Address,Description,Mail,Phone,Small_Description")] School school)
{
if (ModelState.IsValid)
{
db.School.Add(school);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(school);
}
模型:
public partial class School
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public School()
{
this.Product = new HashSet<Product>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Description { get; set; }
public string Mail { get; set; }
public int? Phone { get; set; }
public byte[] Logo { get; set; }
public string Small_Description { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Product> Product { get; set; }
}