我正在尝试从列表中删除或隐藏项目,我面临两个问题,1- 新的无法删除,2- 尝试使用 Javascript 将已删除的项目标记为 isDeleted = true 然后稍后在控制器中删除它们这个答案https://stackoverflow.com/a/40572625/10773318但它没有用。
这是我的视图模型
public class CreateEditParentViewModel
{
public int Id { get; set; }
public IList<ChildViewModel> ChildrenLists { get; set; }
}
public class ChildViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public bool isDeleted { get; set; }
}
在主视图中
<div id="editorRows">
@foreach (var item in Model.ChildrenLists)
{
<partial name="_RowPartial" model="item" />
}
</div>
<a id="addItem" asp-action="BlankRow" asp-controller="Home">Add Row...</a> <br />
<input type="submit" value="Finished" />
主视图中的 javascript
@section scripts {
<script>
$("#addItem").click(function () {
$.ajax({
url: this.href,
cache: false,
success: function (html) { $("#editorRows").append(html); }
});
return false;
});
$("a.deleteRow").click(function () {
$(this).parents("div.editorRow:first").remove(); //does not work with newly added
return false;
}); //what it should do: hide and set isDeleted = true if id is not null - remove if null
</script>
最后是局部视图
<div class="editorRow">
@using (Html.BeginCollectionItem("ChildrenLists"))
{
@Html.HiddenFor(m => m.Id)
@Html.HiddenFor(m => m.isDeleted)
<span>Name: </span> @Html.EditorFor(m => m.Name);
}
<a href="#" class="deleteRow">delete</a>
