patterncsharpMinor
Updating Related Data the MVC/EF Way
Viewed 0 times
themvcupdatingrelatedwaydata
Problem
I have an ASP.NET MVC Controller, the relevant parts of which appear here:
```
public class IncidentController : Controller
{
private IncidentDBContext db = new IncidentDBContext();
public ActionResult Edit(int id = 0)
{
Incident incident = db.Incidents
.Include(i => i.Images)
.Where(i => i.IncidentID == id)
.Single();
if (incident == null)
{
return HttpNotFound();
}
return View(incident);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Incident incident, int[] SelectedImages)
{
if (ModelState.IsValid)
{
UpdateRelatedImages(incident, SelectedImages);
db.Entry(incident).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(incident);
}
private void UpdateRelatedImages(Incident incident, int[] selectedImageIDs)
{
incident.Images = new List();
foreach (int imageID in selectedImageIDs)
{
IncidentImage image = db.IncidentImages.Where(i => i.IncidentImageID == imageID).Single();
if (image.IncidentID == incident.IncidentID)
{
incident.Images.Add(image);
}
else if (image.IncidentID == null)
{
incident.Images.Add(image);
image.IncidentID = incident.IncidentID;
db.Entry(image).State = EntityState.Modified;
}
else
{
throw new InvalidOperationException(String.Format("Cannot assign image {0} to incident {1}. Current Incident ID of image: {2}.", imageID, incident.IncidentID, image.IncidentID));
}
}
}
[HttpPost]
public ActionResult UploadImages()
{
var images = new List();
foreach (string imageName in Request.Files.AllKeys)
{
H
```
public class IncidentController : Controller
{
private IncidentDBContext db = new IncidentDBContext();
public ActionResult Edit(int id = 0)
{
Incident incident = db.Incidents
.Include(i => i.Images)
.Where(i => i.IncidentID == id)
.Single();
if (incident == null)
{
return HttpNotFound();
}
return View(incident);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Incident incident, int[] SelectedImages)
{
if (ModelState.IsValid)
{
UpdateRelatedImages(incident, SelectedImages);
db.Entry(incident).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(incident);
}
private void UpdateRelatedImages(Incident incident, int[] selectedImageIDs)
{
incident.Images = new List();
foreach (int imageID in selectedImageIDs)
{
IncidentImage image = db.IncidentImages.Where(i => i.IncidentImageID == imageID).Single();
if (image.IncidentID == incident.IncidentID)
{
incident.Images.Add(image);
}
else if (image.IncidentID == null)
{
incident.Images.Add(image);
image.IncidentID = incident.IncidentID;
db.Entry(image).State = EntityState.Modified;
}
else
{
throw new InvalidOperationException(String.Format("Cannot assign image {0} to incident {1}. Current Incident ID of image: {2}.", imageID, incident.IncidentID, image.IncidentID));
}
}
}
[HttpPost]
public ActionResult UploadImages()
{
var images = new List();
foreach (string imageName in Request.Files.AllKeys)
{
H
Solution
I am a bit late and I don't know if I understand very well the second part of your question but maybe I can help you with the first one about CreateImage method. If you are using DB generated Ids for your IncidentImage entity, EF follows every INSERT with SELECT SCOPE_IDENTITY(), hence you already have the id
private int CreateImage(string imageName, string fileName)
{
IncidentImage image = new IncidentImage
{
// set image and file name
}
db.IncidentImages.AddObject(image);
db.SaveChanges();
return image.IncidentImageID;
}Code Snippets
private int CreateImage(string imageName, string fileName)
{
IncidentImage image = new IncidentImage
{
// set image and file name
}
db.IncidentImages.AddObject(image);
db.SaveChanges();
return image.IncidentImageID;
}Context
StackExchange Code Review Q#59815, answer score: 3
Revisions (0)
No revisions yet.