I have a search page which allows users to search for an item and add details to an item. I am working on allowing users to apply the same details to several items. The user will check all the items he would like to apply the same changes to and click a Comment button. This will send a list of items checked List<int> ids and the particular item clicked (to use as template in case it already exists) int id.
From here the user will edit the comment however he wishes and save. How will the HttpPost Edit action look? It is required to add a comment to the database if it doesn’t exist for that item already, otherwise, overwrite what exists. Below is what I have come up with (basic outline). The problem I see with this is that it requires poking the database for every item I want the changes applied to. There has to be a better way to do this.
[HttpPost]
public ActionResult Edit(CommentVM model)
{
if (ModelState.IsValid)
{
foreach(int i in model.ids)
{
Comment comment = _db.Comments.Find(i);
if(comment == null){
//Create and add
{
else{
comment.Text = model.Text;
_db.Entry(comment).State = EntityState.Modified;
}
}
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
I ended up using a sort of homebrew method. Since both cases (add entry and edit entry needed to be handled separately, I handled them as such.