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 :
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 :
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 :
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);
}
[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 :
@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" />
@*}*@
}
}