1

Hi everyone I'm trying to add edit and delete but delete doesnt work here is my index code for listing;

@foreach (var item in Model.Haberler)
                            {
                                <tr id="@item.Id" data-trid="[email protected]">
                                    <td>
                                        <a href="javascript:;" class="btn btn-xs dropdown-toggle" data-toggle="sortable">
                                            <i class="fa fa-reorder"></i>
                                        </a>
                                    </td>
                                    <td>
                                        <div class="btn-group">
                                            <button type="button" class="btn btn-xs purple dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-delay="1000" data-close-others="true">
                                                İşlem <i class="fa fa-angle-down"></i>
                                            </button>
                                            <ul class="dropdown-menu" role="menu">
                                                <li><a href="@Url.Action("Edit", new { Id = item.Id })" data-title="Kayıt Güncelle"><i class="fa fa-edit"></i>Düzenle</a></li>
                                                <li><a href="javascript:;" data-toggle="bsdelete" data-title="@item.Title" data-url="@Url.Action("Delete")" data-id="@item.Id"><i class="fa fa-trash-o"></i>Sil</a></li>
                                            </ul>
                                        </div>
                                    </td>
                                    <td><a href="@item.Resim" data-rel="fancybox-button" class="fancybox-button">@item.Title</a></td>
                                </tr>
                            }

Here is my controller Delete;

public ActionResult Delete(int? Id)
    {
        if (!Id.HasValue)
            return Json(new { IsComplete = false });

        try
        {
            var item = Db.Haberler.FirstOrDefault(p => p.Id == Id.Value);
            UrunSil(item);
        }
        catch
        {
            return Json(new { IsComplete = false });
        }

        return Json(new { IsComplete = true });
    }

And I send to AdminBaseController to delete ;

   public void DosyaSil(string yol)
    {
        try
        {
            System.IO.File.Delete(Server.MapPath("~" + yol));
        }
        catch { }
    }
    public void UrunSil(News item)
    {

        DosyaSil(item.ResimBuyuk);
        DosyaSil(item.ResimKucuk);
        Db.Haberler.Remove(item);
        Db.SaveChanges();
    }

Break points doesnt work when I click on delete icon it does nothing just like empty what could be the problem ?

When I click the delete button I get this error on console;

Uncaught ReferenceError: bootbox is not definedglobal.js:113 (anonymous function)jquery-1.10.2.min.js:5 x.event.dispatchjquery-1.10.2.min.js:5 v.handle
7
  • do this after you remove the item Db.Entry(item).State=EntityState.Deleted; Commented Jan 8, 2015 at 13:00
  • Still Same I think something wrong with finding the delete in controller Commented Jan 8, 2015 at 13:02
  • 2
    Your 'delete' option is actually an anchor tag, but href is not actually pointing to anywhere. is it not required to have the url? Commented Jan 8, 2015 at 13:04
  • @Thangadurai is right, Your anchor link is not pointing any method! If you could see in console it should be like- <a href="/Delete"></a> Commented Jan 8, 2015 at 13:06
  • @Thangadurai I think I'm sending it with URl.Action("Delete") but it doesnt work as I see Commented Jan 8, 2015 at 13:07

2 Answers 2

1

The delete isn't work beacause The delete method isn't work beacause you is calling the wrong way, if you wanna call a delete action like this you need to redirect to another action process and return to "the Current View/Action". You can do this really simple with Ajax and Jquery, this is a sample:

    <li><a class="delete" data-toggle="bsdelete" data-title="@item.Title" data-url="@Url.Action("Delete")" data-id="@item.Id"><i class="fa fa-trash-o"></i>Sil</a></li>

And the js you need to do this:

 $(function(){
    $(".delete").click(function () {
       $.ajax({
           type: "DELETE",
           url: $(this).attr("data-url") + "?Id=" + $(this).attr("data-id"),
           success: function (data) {
             //Remake the search
          },
          error: function(jqXHR, exception, a, b){
                 alert(jqXHR.responseText);
           }
      });
   });
});

The Delete method don't recive the body of message and the Http verb use is Delete because the name of your action. So the parameter need to be in queryString on the Url.

Sign up to request clarification or add additional context in comments.

3 Comments

Still no alert just Uncaught ReferenceError: bootbox is not defined in console.I dont even get error function I dont know why
try put this in the header of your page: <script src="//cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.3.0/bootbox.min.js" language="javascript" type="text/javascript"></script>
Yes I've seen that my bootbos.min.js was curropted and this fixed my issue thank you =)
0

Add class name for <a> as below:

<li><a href="javascript:;" class="delete" data-toggle="bsdelete" data-title="@item.Title" data-    url="@Url.Action("Delete")" data-id="@item.Id"><i class="fa fa-trash-o"></i>Sil</a></li>

Use ajax to call the controller as below :

$(function(){
   $(".delete").click(function () {
     $.ajax({
       url: $(this).attr("data-url"),
        data: "Id="+$(this).attr("data-id"),
       success: function (data) {
        // do stuff
       }
     });
  });
});

3 Comments

I've also tried that and Also now I only made an alert when its called but still nothing doesnt even alert
check your firebug if any javascript errors are there?
check preference of you js files. Order should be First Jquey file and the other js files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.