Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have made an multiple upload af image and it works just fine. But how do I make an edit? I have a student model that can have multiple images, how do I get the value from that picture I want to change?

is it possible to get the values from the [HttpGet] which photos there is in my edit view and how can I check which photo I have selected?

This is my Photo model :
C#
public class Photo
    {
        public int PhotoId { get; set; }

        public String ProfileImagePath { get; set; }

        public int StudentId { get; set; }

        public virtual Student Student { get; set; }

        public void SaveImage(HttpPostedFileBase image,
            String serverPath, String pathToFile)
        {
            if (image == null) return;

            string filename = Guid.NewGuid().ToString();
            ImageModel.ResizeAndSave(
                serverPath + pathToFile, filename,
                image.InputStream, 200);

            ProfileImagePath = pathToFile + filename +
            ".jpg";
        }


    }



The Student model :

C#
public class Student
    {
        public int StudentId { get; set; }

        public String Name { get; set; }

        public String Adress { get; set; }

        public virtual ICollection<Photo> Photos { get; set; }

    }

Thise is my edit in StudentsController :

C#
public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

C#
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "StudentId,Name,Adress, PhotoId, ProfileImagePath, StudentId")] Student student, HttpPostedFileBase image, Photo photoFile, FormCollection _collection)
{
    if (ModelState.IsValid)
    {
        var hiddenId = _collection["photoId"];
        var photoPath = _collection["photoPath"];


        if (student.StudentId == default(int))
        {
            db.Students.Add(student);
            photoFile.SaveImage(image, HttpContext.Server.MapPath("~"), "/ProfileImages/");

        }
        else
        {
            db.Entry(student).State = EntityState.Modified;
            photoFile.SaveImage(image, HttpContext.Server.MapPath("~"), "/ProfileImages/");

        }
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(student);
}


The view for edit :

C#
@model test4.Models.Student


        @foreach (var item in Model.Photos)
        {
                          
                @Html.Hidden("photoId", item.PhotoId);
              

                 <img src="@item.ProfileImagePath" alt="photo"/>
              
                 <input type="file" name="image" multiple="multiple" value="@item.PhotoId"  />
             @*}*@
        }
      
}
Posted
Updated 23-Oct-15 0:06am
v3
Comments
Sreekanth Mothukuru 21-Oct-15 7:02am    
Can't you make use of PhotoId as it is going to be unique per photo to identify which photo is being edited?!!
tina_overgaard 21-Oct-15 7:41am    
I dont know how to get the id from the picture I want to edit if I clicked on the input type="file"
Sreekanth Mothukuru 21-Oct-15 8:29am    
You just need to post the PhotoId along with the new posted file to replace the existing one. I think there will be no edit option to the existing photo.
tina_overgaard 21-Oct-15 10:54am    
Can you maybee show me how I do that?
Sreekanth Mothukuru 23-Oct-15 2:50am    
You need to store the PhotoId on the view either in hidden field so that you can pass it along with the input file content to the server to identify the record to replace old pic with the new one. Check the link below from Stack Overflow to accomplish this

http://stackoverflow.com/questions/20629105/ajax-fileupload-jquery-formdata-in-asp-net-mvc

1 solution

Hi, I am agree with Sreekanth, as he said:
You need to create different hidden fields within the loop based on index. like : <input type="hidden" name="hid"+ @item.index++ value="@item.PhotoId" />

Then you need to use the id of edited image and update the same image content in Action of your Controller.

I have not done the same but may be my article would help a little by giving you update scenario for single image:
Uploading Image by File Browsing, Dragging & Dropping and Using Webcam in ASP.NET MVC[^]

Thanks.
 
Share this answer
 
v2
Comments
tina_overgaard 1-Nov-15 15:50pm    
I have tried this, but what is the @item.index?? I have tried to search on the internet for that soloution
Snesh Prajapati 1-Nov-15 20:09pm    
That would be unique ID for each item, so that we can distinguish and update right image.
tina_overgaard 3-Nov-15 4:36am    
I have tried this one <input type="hidden" name="hid"+ @item.PhotoId++ value="@item.PhotoId" /> But it still gives me the wrong Id. If I take the last picture it will take the first pictures Id

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900