C# 从另一个模型中删除项目

C# 从另一个模型中删除项目,c#,asp.net,asp.net-mvc,asp.net-mvc-5,entity-framework-6,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 5,Entity Framework 6,我正在尝试使用asp.NETMVC构建一个简单的todo应用程序。 我的应用程序有两种型号: 1个用于列表(ToDoLijst.cs) 现在我有了一个视图,其中列表显示了它们的项目,这很好。这是ToDoLijst视图的Index.cshtml <div class="container lijsten"> @foreach (var lijst in Model.Lijsten) { <div class="col-lg-5 lijst"> <

我正在尝试使用asp.NETMVC构建一个简单的todo应用程序。 我的应用程序有两种型号: 1个用于列表(ToDoLijst.cs)

现在我有了一个视图,其中列表显示了它们的项目,这很好。这是ToDoLijst视图的Index.cshtml

<div class="container lijsten">
@foreach (var lijst in Model.Lijsten)
{
    <div class="col-lg-5 lijst">
        <div class="row">
            <div class="col-lg-11">
                <h3>
                    @Html.DisplayFor(modelItem => lijst.Titel)
                </h3>
            </div>
            <div class="col-lg-1">
                @using (Html.BeginForm("Delete", "ToDoLijsten", new { id = Html.DisplayFor(modelItem => lijst.LijstId) }))
                {
                    @Html.AntiForgeryToken()
                    <div class="form-actions no-color pull-right">
                        <button type="Submit" value="Delete" class="btn btn-xs btn-default btn-close"><span class="glyphicon glyphicon-remove"></span></button>
                    </div>
                }
            </div>
        </div>
        @foreach (var item in Model.LijstItems)
        {
            if (item.LijstId == lijst.LijstId)
            {<div class="row">
                <div class="col-lg-11">
                    <p>@Html.DisplayFor(modelItem => item.Item)</p>
                </div>
                <div class="col-lg-1">
                    <a href="@Url.Action("Delete", "LijstItems", new { id = item.LijstItemId})"><span class="glyphicon glyphicon-remove"></span></a>
                </div>
            </div>
            }
        }
    </div>
}

@foreach(模型中的var lijst.Lijsten)
{
@DisplayFor(modeleItem=>lijst.Titel)
@使用(Html.BeginForm(“Delete”,“ToDoLijsten”,new{id=Html.DisplayFor(modeleItem=>lijst.LijstId)}))
{
@Html.AntiForgeryToken()
}
@foreach(Model.LijstItems中的var项)
{
如果(item.LijstId==lijst.LijstId)
{
@DisplayFor(modelItem=>item.item)

} } }

我可以将列表作为一个整体删除。现在我希望能够从列表中逐个删除项目。我使用下面的代码来完成这项工作,它将我带到LijstItems视图的“Dele”视图。 有没有办法直接删除这些邮件

这是我使用的代码:

<a href="@Url.Action("Delete", "LijstItems", new { id = item.LijstItemId})"><span class="glyphicon glyphicon-remove"></span></a>


Thnx

我从您的问题中了解到,您希望删除记录,而不必进入删除页面:

为此:

@foreach (var item in Model.LijstItems)
        {
            if (item.LijstId == lijst.LijstId)
            {<div class="row">
                <div class="col-lg-11">
                    <p>@Html.DisplayFor(modelItem => item.Item)</p>
                </div>
                <div class="col-lg-1">
                    <input type="button" value="Delete" id="deleteButton" data-id="@item.LijstItemId"/>
                </div>
            </div>
            }
        }


<script>
$("#deleteButton").click(function(e){
     e.preventDefault();
     var id=$(this).data("id");
     $.post('@Url.Action("DeleteDirectly","YourController")',new {id:id},function(data){
         if(data.status){
            alert("Delete succeeded!");             
         }
         else{
            alert("Delete failed!");
         }
     });
});
</script>

希望它能帮助您

您能从控制器中发布代码吗?您的删除操作不需要返回视图,它只需返回一条简单的JSON确认消息,在ajax发布后,客户端即可显示该消息。
<a href="@Url.Action("Delete", "LijstItems", new { id = item.LijstItemId})"><span class="glyphicon glyphicon-remove"></span></a>
@foreach (var item in Model.LijstItems)
        {
            if (item.LijstId == lijst.LijstId)
            {<div class="row">
                <div class="col-lg-11">
                    <p>@Html.DisplayFor(modelItem => item.Item)</p>
                </div>
                <div class="col-lg-1">
                    <input type="button" value="Delete" id="deleteButton" data-id="@item.LijstItemId"/>
                </div>
            </div>
            }
        }


<script>
$("#deleteButton").click(function(e){
     e.preventDefault();
     var id=$(this).data("id");
     $.post('@Url.Action("DeleteDirectly","YourController")',new {id:id},function(data){
         if(data.status){
            alert("Delete succeeded!");             
         }
         else{
            alert("Delete failed!");
         }
     });
});
</script>
public JsonResult DeleteDirectly(int id)
{
     // you do the delete from database
     // if deleted successfully from database then write the following code
     return Json(new {status=true});
     // if deletion was failed for any reason, then return the following code
     return Json(new {status=false});
}