PDA

View Full Version : سوال: عدم ثبت عکس در بانک اطلاعاتی



Iran58
سه شنبه 03 بهمن 1396, 17:14 عصر
سلام
من کلاسهای زیر را نوشته ام


public class Brand
{
public Brand()
{
PicId=new Guid();
}
[Key]
public int BrandId { get; set; }
[Display(Name = "تصوير", Prompt = "تست1", Description = "تست2")]
[Column(TypeName = "image")]
public byte[] BrandImage { get; set; }
public Guid? PicId { get; set; }
}
public class Context:DbContext
{
public Context()
{

}


public DbSet<Brand> Brands { get; set; }
}



و کد کنترل مربوطه را هم بصورت زیر نوشته ام




[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create( HttpPostedFileBase imFileBase)
{
if (ModelState.IsValid)
{
if (imFileBase != null)
{
var uploadFile = new byte[imFileBase.InputStream.Length];
imFileBase.InputStream.Read(uploadFile, 0, uploadFile.Length);
var brand = new Brand
{
BrandImage = uploadFile
};
db.Brands.Add(brand);
db.SaveChanges();
return RedirectToAction("Index");
}
}


return View();
}

و کد view هم بصورت زیر است



@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()


<div class="col-md-12">
<div class="col-md-2">
<input type="file" name="Image" />
</div>
<div class="col-md-3">
<input type="submit" value="ذخیره عکس" />
</div>
</div>
}



اما هنگام ثبت imFileBase را برابر null قرار میدهد
مشکل کد من کجاست ایا روش دیگری هم وجود دارد

Moien Tajik
سه شنبه 03 بهمن 1396, 18:26 عصر
name اون input که داخل view قرار دادید باید با پارامتر ورودی اکشنتون همنام باشه تا Binding صورت بگیره.

طبق این آموزش جلو برید : https://www.mikesdotnetting.com/article/259/asp-net-mvc-5-with-ef-6-working-with-files