1

am using .netcore 1.1 , Visual studio 2017 , Jquery version = "3.2.1"

,am trying to make the MVC controller to get data from my page , i have 2 arrays in java , one is an array of Object (model view) and one is an array of strings

  1. objects array always return error 400 (bad request) 2- string array ,always send null to the controller

i followed the below answers with no success

https://stackoverflow.com/a/13255459/6741585

and

https://stackoverflow.com/a/18808033/6741585

below is my chtml page

     //#region send data back t oserver
        $('#Savebtn').click(function () {
            console.log(editedRows);

            var UpdatedRows = JSON.stringify({ 'acActivityed': editedRows });
            console.log(UpdatedRows);

            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "/acActivity/Edit",
                //traditional: true,   
                data: UpdatedRows ,
                dataType: "json",
                success: function (data) {
                    // here comes your response after calling the server
                    alert('Suceeded');
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert("error : " + jqXHR.responseText);
                }
            });
        });
        //#endregion

        //#region deleted all selected
        $('#DeleteSelectedbtn').on('click', function () {
            if (confirm("Are you sure to delete All Selected ?????")) {
                var selectedData = [];
                 var selectedIndexes;

                    selectedIndexes = grid.getSelectedRows();
                    jQuery.each(selectedIndexes, function (index, value) {
                        selectedData.push(grid.getDataItem(value).id);
                    });
                    console.log(selectedData);
                    var SelectedIds = selectedData.join(',') ;
                    console.log(SelectedIds);
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "/acActivity/DeleteSelected",
                    data: JSON.stringify({ "ids": SelectedIds }),
                    dataType: "json",
                    success: function (data) {
                        grid.render();
                    },
                    error: function (err) {
                        alert("error : " + err);
                    }
                });
            }
        });
        //#endregion

and both do have data as below console shots the selected ID's one Object Array  from Console

ID's Selected from Console

my Controller this one should expect the list of object and always return bad request ,

    [HttpPost]
    [ValidateAntiForgeryToken]
    //public jsonResult Edit( List<acActivitytbl> acActivityed)
    public ActionResult Edit( List<acActivitytbl> acActivityed)
    {
       foreach (acActivitytbl it in acActivityed)
        {
            logger.Log(LogLevel.Info, it.ID);
            logger.Log(LogLevel.Info, it.Name);
            logger.Log(LogLevel.Info, it.IsActive);
        }


        //return View(acActivityed);
        return Json(new { success = true, responseText = "end of Page" });
    }

that one should expect the delimited string ,but always receive null

    public ActionResult DeleteSelected(string ids)
    {
        try
        {
            char[] delimiterChars = { ' ', ',', '.', ':', ' ' };


            string[] words = ids.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);

            if (words != null && words.Length > 0)
            {

                foreach (var id in words)
                {

                    bool done = true; //new acActivitiesVM().Delete(Convert.ToInt32(id));
                    logger.Log(LogLevel.Info, " acActivities ID {0} is Deleted Scussefully  ", id);
                }
                return Json(new { success = true, responseText = "Deleted Scussefully" });

            }
            return Json(new { success = false, responseText = "Nothing Selected" });
        }
        catch (Exception dex)
        {
    ..... removed to save space 
     });
        }
    }

i know there is something missing here ,but i cannot find it ,any help in that ??

Ali
  • 1,080
  • 16
  • 22
  • Its just `data: UpdatedRows,` (`UpdatedRows` is already you correct data) and you can remove `traditional: true,` (that is not applicable for collections of complex objects or your `contentType` option. –  May 28 '17 at 21:10
  • thanks ,i did that exactly and still getting same error (400 (Bad Request)) – Ali May 28 '17 at 21:22
  • Use your browser tools to inspect the response (Network tab) to determine what the error is. And with the 2nd example, why are you taking an array, joining it to a `string` and then splitting it back again in the controller?) –  May 28 '17 at 21:26
  • for the 2nd one , am trying every thing ,hoping that a try may make things go better ,so i tried to send it as string in one of them ,and kept it there as is , – Ali May 28 '17 at 21:40
  • In 2nd case no need to join , you can send list of string id to controller List id – Asif Raza May 28 '17 at 22:39
  • In 1st case 400 (bad request) mean you sending bad parameter to your controller .. Make sure are you sending all the parameter to controller ? – Asif Raza May 28 '17 at 22:41

0 Answers0