使用jQuery Ajax将对象数组传递到MVC .net核心控制器方法

时间:2017-05-28 21:06:44

标签: javascript jquery ajax asp.net-mvc asp.net-core

我正在使用.netcore 1.1,Visual Studio 2017,Jquery version =“3.2.1”

,我试图让MVC控制器从我的页面获取数据, 我在java中有2个数组,一个是Object数组(模型视图),另一个是字符串数组

  1. 对象数组总是返回错误400(错误请求) 2-字符串数组,总是向控制器发送null
  2. 我按照以下答案没有成功

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

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

    下面是我的chtml页面

         //#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
    

    并且两者都有如下控制台镜头的数据 选定的ID是一个 Object Array  from Console

    ID's Selected from Console

    我的控制器 这个应该期望对象列表并且总是返回错误请求,

        [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" });
        }
    

    应该期望分隔的字符串,但始终接收空

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

    我知道这里缺少一些东西,但我找不到它,有什么帮助吗?

0 个答案:

没有答案