1

I want to use ajax to delete row from database and after delete make refresh for partial view that contain all data

<a class="fa fa-times" href="javascript:DeleteCurrency(@item.CountryID)" title="Delete"></a>

**This is the anchor link that will call javascript function **

 function DeleteCurrency(CountryID) {
        var Con = confirm("are you sure ?");
        if (Con == true) {
            $.ajax({
                url: "@Url.Action("DeleteCountry","Main")" + CountryID,
                type: 'Delete',
                async: true,
                processData: false,
                cache: false,
                sucess: function (data) {
                    alert(dtat);
                },

                error: function (xhr) {
                    alert('error');

                }


            });
        }

    }

**And this is my controller method for delete **

 [HttpDelete]
    public ActionResult DeleteCountry(int CountryID)
    {
        try
        {
            Country count = db.Countrys.Find(CountryID);
            db.Countrys.Remove(count);
            db.SaveChanges();
            var AllCountries = (from cur in db.Countrys orderby cur.govCode ascending select cur).ToList();
            ViewBag.Count = (from cur in db.Countrys orderby cur.govCode ascending select cur).ToList().Count();
            return PartialView("_DetailsOfData", AllCountries);
        }
        catch (Exception)
        {
            ViewBag.AlreadyAdded = "يوجد محافظات تابعه لهذه الدوله لا يمكن مسحها";
            var AllCountries = (from cur in db.Countrys orderby cur.govCode ascending select cur).ToList();
            ViewBag.Count = (from cur in db.Countrys orderby cur.govCode ascending select cur).ToList().Count();
            return View("AddCountry", AllCountries);
        }
    }

when i hit anchor link confirm message popup (are you sure?) when click ok all time it enter in error i want to know what is the error

Amgad Mohamed
  • 163
  • 1
  • 5
  • 15
  • use `dataType:'html'` in ajax options as you are returning html. – Kartikeya Khosla Apr 06 '16 at 11:35
  • Then show the error so that people can help you solve it – Kishore Barik Apr 06 '16 at 11:37
  • @KishoreBarik it shows him a alert with text 'error'...he handled the ajax error that way only..... – SamGhatak Apr 06 '16 at 11:39
  • @ProAmgad try hitting the url with specific data from browser(without ajax)...it will display the error...then upload the error you receive here – SamGhatak Apr 06 '16 at 11:42
  • @KishoreBarik i mean with error that all time the function enter in else – Amgad Mohamed Apr 06 '16 at 11:43
  • @SamGhatak can you describe some details what you mean ?in ajax call there is success and error call with ancor all time enter error why – Amgad Mohamed Apr 06 '16 at 11:47
  • you need to know what is the error, for that refer jquery ajax docs to how to get the error, else better you use your browser developer tools to see the network profile. There you can get all the valuable information regarding the request. Check whether you are sending all required parameters correctly (if you are sure about your API) – Kishore Barik Apr 06 '16 at 11:49
  • exactly what @KishoreBarik explained..,and that is a better approach – SamGhatak Apr 06 '16 at 11:50
  • Please take a look at my answer in http://stackoverflow.com/questions/19093603/simple-post-to-web-api/33667496#33667496 it talks about `POST` action, but same applies for `DELETE` action as well. basically, `url: '@Url.Action("DeleteCountry","Main")' + CountryID` needs to be changed to `url: '@Url.Action("DeleteCountry","Main")'` and pass CountryId as data to the ajax request `data: {'':CountryId}`. Then in your action change the method signature to `public ActionResult DeleteCountry([FromBody] int CountryID)` – Amila Apr 06 '16 at 12:26
  • yes i do this except **[formbody]** i no understand what is the benefit of using it and why this action don't redirect to controller as excepted – Amgad Mohamed Apr 06 '16 at 12:38

1 Answers1

0

CountryID should be passed separately in ajax data attribute. For example:

    function DeleteCurrency(CountryID) {
            var Con = confirm("are you sure ?");
            if (Con == true) {
                $.ajax({
                    url: "@Url.Action("DeleteCountry","Main")" ,
                    type: 'Delete',
                    data: {
                        CountryID: CountryID,
                    },
                    dataType: "text/html",
                    async: true,
                    cache: false,
                    sucess: function (data) {
                        alert(dtat);
                    },

                    error: function (xhr) {
                        alert('error');

                    }


                });
            }

        }
waqas
  • 88
  • 2
  • 8