How to change saved image by uploading image again in View Edit (Asp.net MVC)

0

I'm developing a web application and loading image using an external class of utility name , in ActionResult Create I can upload and save image to Directory but I'm having trouble changing the image saved through ActionResul Edit . Somebody help me, please. for a better understanding I will put below the external class and how I used it in the action create and the difficulty that I have in the action edit

Utility class

namespace GesTark.Classes
{

    public class Utilidades
    {
        private static ApplicationDbContext userContext = new ApplicationDbContext();
        private static GestarkContext db = new GestarkContext();

         public static string UploadPhto(HttpPostedFileBase file)
            {
                //carregar Imagem
                string path = string.Empty;
                string pic = string.Empty;

                if (file != null)
                {
                    pic = Path.GetFileName(file.FileName);
                    path = Path.Combine(HttpContext.Current.Server.MapPath("~/Content/Fotos"), pic);
                    file.SaveAs(path);

                    using (MemoryStream ms = new MemoryStream())
                    {
                        file.InputStream.CopyTo(ms);
                        byte[] array = ms.GetBuffer();
                    }
                }

                return pic;
            }}

PersonaView class

namespace GesTark.Models
{
    public class PessoaView
    {
        public Pessoa Pessoa { get; set; }

        public ArquivosGuardados ArquivosGuardado { get; set; }

        public AssuntoGeral AssuntoGeral { get; set; }

        public Provincia Provincia { get; set; }

        public Gabinetes Gabinetes { get; set; }

        public HttpPostedFileBase Foto { get; set; }

        public HttpPostedFileBase Ficheiro { get; set; }
    }
}

ACTION Create - within view Create I called @model GesTark.Models.PessoaView Example

                 @ Html.LabelFor (model => model.Person.Name, htmlAttributes:   new {@class="control-label col-md-2"})                                  @ Html.EditorFor (model => model.Person.Name,   new {htmlAttributes = new {@class="form-control"}})                   @Html.ValidationMessageFor (model => model.Person.Name, "", "   new {@class="text-danger"})                          

_---------------------------------------------- ----------------------------

public ActionResult Create()
        {
            ViewBag.Nivel = new SelectList(db.Nivels, "NivelId", "Nome");
            ViewBag.Gabinetes = new SelectList(db.Gabinetes, "GabineteId", "Nome");
            return View();
        }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(PessoaView view)
    {
        ViewBag.Nivel = new SelectList(db.Nivels, "NivelId", "Nome",view.Pessoa.NivelNivelId);
        ViewBag.Gabinetes = new SelectList(db.Gabinetes, "GabineteId", "Nome",view.Pessoa.GabinetesGabineteId);

        if (ModelState.IsValid)
        {

            db.Pessoas.Add(view.Pessoa);
            try
            {

                if (view.Foto != null)
                {
                    var pic = Utilidades.UploadPhto(view.Foto);
                    if (!string.IsNullOrEmpty(pic))
                    {
                        view.Pessoa.Photo = string.Format("~/Content/Fotos/{0}", pic);
                    }
                }


                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {

                ModelState.AddModelError(string.Empty, ex.Message);
            }
        }

        return View(view);
    }

ACTION EDIT, HERE IS MY PROBLEM, IF EXECUTED, COMES THIS ERROR ATTACHED UNDER THE CODE, AND I DO NOT KNOW HOW TO RESOLVE

**

  

ACTION EDIT

**

public ActionResult Edit(int? id)
    {

        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        Pessoa pessoa = db.Pessoas.Find(id);

        if (pessoa == null)
        {
            return HttpNotFound();
        }

        ViewBag.Nivel = new SelectList(db.Nivels, "NivelId", "Nome", pessoa.NivelNivelId);
        ViewBag.Gabinetes = new SelectList(db.Gabinetes, "GabineteId", "Nome", pessoa.GabinetesGabineteId);


        return View(pessoa);
    }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(PessoaView view)
    {
        if (ModelState.IsValid)
        {

                db.Entry(view).State = EntityState.Modified;
            try
            {

                if (view.Foto != null)
                {
                    var pic = Utilidades.UploadPhto(view.Foto);
                    if (!string.IsNullOrEmpty(pic))
                    {
                        view.Pessoa.Photo = string.Format("~/Content/Fotos/{0}", pic);
                    }
                }


                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {

                ModelState.AddModelError(string.Empty, ex.Message);
            }

        }


        ViewBag.Nivel = new SelectList(db.Nivels, "NivelId", "Nome", view.Pessoa.NivelNivelId);
        ViewBag.Gabinetes = new SelectList(db.Gabinetes, "GabineteId", "Nome", view.Pessoa.GabinetesGabineteId);

        return View(view);
    }

    
asked by anonymous 07.06.2018 / 06:35

0 answers