c# - Delete Item from another Model -



c# - Delete Item from another Model -

i'm trying build simple todo-app using asp.net mvc. app has 2 models: 1 lists (todolijst.cs)

namespace onslijstje.models { public class todolijst { [key] public int lijstid { get; set; } public string titel { get; set; } public virtual icollection<lijstitem> lijstitems { get; set; } } }

and 1 items (lijstitems.cs)in list.

namespace onslijstje.models { public class lijstitem { [key] public int lijstitemid{ get; set;} public int lijstid { get; set; } public string item { get; set; } public virtual todolijst todolijst { get; set; } } }

now have view lists shown items , works fine. index.cshtml of todolijst views.

<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> }

i can delete lists whole. bud want able delete items 1 1 list. utilize code below bud brings me view 'dele' of lijstitems views. there way can items deleted directly

this code use:

<a href="@url.action("delete", "lijstitems", new { id = item.lijstitemid})"><span class="glyphicon glyphicon-remove"></span></a>

thnx

what understood question want delete record without going delete page:

to this:

@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>

in controller

public jsonresult deletedirectly(int id) { // delete database // if deleted database write next code homecoming json(new {status=true}); // if deletion failed reason, homecoming next code homecoming json(new {status=false}); }

hope help you

c# asp.net asp.net-mvc asp.net-mvc-5 entity-framework-6

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -