I am using BeginCollectionItem to dynamically add items (in this case, Comments) to a list. In attempting to "remove" an item I have run into an issue. I can remove an 'existing' item from the UI using jQuery without issue using the following code:
var container = $(this).closest('.editorRow');
var id = container.data('id');
if (!id) {
$(this).parents("div.editorRow:first").remove();
e.preventDefault();
e.stopImmediatePropagation();
return false;
}
But that code will not remove an item that has just been added to the view and not already saved to the database.
The other problem is, I don't want to remove it from the UI until it is removed from the database. I tried using the same code in the success function of the ajax call but now it will not remove the div from the UI. The ajax call works, but when it his the remove line, it does not update the UI.
else {
$.ajax({
type: "POST",
url: "@Url.Action("Delete","Request", new { area = "" })",
data: { CommentId: id },
success: function () {
$(this).parents("div.editorRow:first").remove();
}
})
}
Update
Changing the ajax portion to this allowed for the deleting of the div in the UI, but now after briefly showing me that it deleted the div it redirects me to the login page of my app.
else {
$.ajax({
type: "POST",
url: "@Url.Action("Delete","Request", new { area = "" })",
data: { CommentId: id },
success: function (e) {
$t.parents("div.editorRow:first").remove();
e.preventDefault();
e.stopImmediatePropagation();
return false;
}
})
}