Entity Framework 5 Code first adding an image

前端 未结 1 939
臣服心动
臣服心动 2020-12-30 12:05

I am writing a very small application with mvc4 and entity framework 5.

I want to add a product and store and image for the product.

I have a model



        
相关标签:
1条回答
  • 2020-12-30 12:53

    Try to fix like that:

    1 . Replace

    <input name="Image" type="file"/> with <input name="ImageFile" type="file"/>

    2 . In controller:

            [HttpPost]
            public ActionResult Create(CatalogItemModel catalogitemmodel, HttpPostedFileBase ImageFile)
            {
                using (var ms = new MemoryStream())
                {
                    ImageFile.InputStream.CopyTo(ms);
                    catalogitemmodel.Image =  ms.ToArray();
                }
    
                if (ModelState.IsValid)
                {
                    db.CatalogItemModels.Add(catalogitemmodel);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
    
                return View(catalogitemmodel);
            }
    
    0 讨论(0)
提交回复
热议问题