I seem to have some problems wrapping my head around the concept about how to best handle updating and deleting from lists in MVC. I’ll try to explain my problem with a short example. From the Entity Framework I have my object Position:
public int Quantity { get; set; }
public int PositionNumber { get; set; }
public string ArticleNumber { get; set; }
public string Name { get; set; }
public string Remark { get; set; }
Now I’ve heard that it’s best to make a ViewModel foreach View with the used properties so to make it simple the ViewModel looks the same. In the controller we return a list of Positions to the view by mapping the Model to the ViewModel.
Now the view displays textboxes for Quantity and Remark which the user can change. He can also delete a position.
Now when posting the data the user entered I do something like this:
[HttpPost]
public ActionResult Index(IEnumerable<ShoppingCartPositionModel> shoppingCartPositions)
{
var positions = _shoppingCartManager.GetActiveShoppingCart().Positions.Values.OrderBy(p => p.PositionNumber).ToList();
if (FormCollection.AllKeys.Any(k => k.Contains("Remove")))
{
string key = FormCollection.AllKeys.Where(k => k.Contains("Remove")).First();
int start = key.LastIndexOf('[') + 1;
int end = key.LastIndexOf(']');
int difference = end - start;
int index;
if (int.TryParse(key.Substring(start, difference), out index))
{
_shoppingCartManager.DeleteShoppingCartPosition(positions.ElementAt(index));
}
}
else
{
if (ModelState.IsValid)
{
for (int i = 0; i < positions.Count(); i++)
{
var position = positions.ElementAt(i);
var cartPosition = shoppingCartPositions.ElementAt(i);
position.PositionNumber = cartPosition.PositionNumber;
position.Quantity = cartPosition.Quantity;
position.Remark = cartPosition.Remark;
}
_shoppingCartManager.UpdateShoppingCartPositions(positions);
}
}
var cartPositions = GetShoppingCartPositionsModel();
UpdateModel(cartPositions);
return View(cartPositions.ToList());
}
My problem is how will this work if in the meantime another user has deleted an object or even added a new position inbetween the others? Would it be best to make a hidden field on the view and map by that? And to detect updates save a changed date and display a hidden field on the view for that aswell? Because I think if I’d simply check the date update like this I would always have the newest update date and I don’t think in the few seconds the service needs to update an item any other user can change the data.
I hope this explaines my issue and thanks to those trying to help me find a way to do this.
The first problem you seem to have is that you have different actions on the same form. Would it help if you could handle the submit buttons for delete and update by different actions? There is a nice solution about this here.
The other point is the detection of possible changes by other users in the meantime. What about a hidden field on the view with the last update time of the whole data structure? If it is still the same on postback, nobody has touched the data.