Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MVC.net上带有Ajax调用控制器函数的Javascript函数不起作用_Javascript_Jquery_Ajax_Regex_Asp.net Mvc - Fatal编程技术网

MVC.net上带有Ajax调用控制器函数的Javascript函数不起作用

MVC.net上带有Ajax调用控制器函数的Javascript函数不起作用,javascript,jquery,ajax,regex,asp.net-mvc,Javascript,Jquery,Ajax,Regex,Asp.net Mvc,当我按下一个按钮执行此功能时,我会在一个数组中保存选中时表格行中的一些参数,一旦完成,我会调用ajax转到我的“controllers”文件夹中名为insertarfaltas的函数,问题是我无法到达那里 我得到的唯一输出是未破坏的 SyntaxError:提供给RegExp构造函数的标志无效 “insertarfaltas” 怎么了?我只想将一个数组传递给控制器函数insertarfaltas $('#guardarfaltas').click(function() {

当我按下一个按钮执行此功能时,我会在一个数组中保存选中时表格行中的一些参数,一旦完成,我会调用ajax转到我的“controllers”文件夹中名为insertarfaltas的函数,问题是我无法到达那里

我得到的唯一输出是未破坏的

SyntaxError:提供给RegExp构造函数的标志无效 “insertarfaltas”

怎么了?我只想将一个数组传递给控制器函数insertarfaltas

 $('#guardarfaltas').click(function() {
            var i = 0;
            var alumnes = [];
            var fechactual = SaberFechaActual();
            $('#tablaalumnos tr').each(function () {
                var id = $(this).find("td").eq(0).html();
                var checkbox = $(this).find("td").eq(2).html();
                var comentario = $(this).find("#comentario").val();

                if ($(this).find("input[type=checkbox]").is(":checked")) {
                    alumnes[i] = [id,comentario,fechactual,hora,codiuf,codicurs,codimodul];
                    i++;
                }
            });

            alumnes = JSON.stringify(alumnes);
            console.log(alumnes);


            $.ajax({
                url: @Url.Action("insertarfaltas","Faltas"),
                type: 'POST',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                traditional: true,
                data: {alumnes : alumnes},
                success: function (data) {
                    console.log(data);
                }
            });
----控制器文件夹:

[HttpPost]
        [AllowAnonymous]
        public String insertarfaltas(String[] alumnes)
        {

            String[] a = alumnes;
            return ("hola ionah");


        }

我不确定问题到底是什么,因为我不确定您是否发布了相关代码,但正如我在评论中所说的,我已经确定您的数组不是
String[]
类型,它更像
array[array[String]
类似于多维数组或其他类型

但是,我会尝试更改对对象的绑定,因为这在代码中更具描述性

行动

public ActionResult AnotherAction(List<MyRequestModel> model)
{

}
然后将JavaScript声明更改为以下内容:

alumnes[i] = { 
               Id: id,
               Comentario: comentario
               Fechactual: fechactual,
               Hora: hora,
               Codiuf: codiuf,
               Codicurs: codicurs,
               Codimodul: codimodul
};

尝试调用JSON.stringify,如下所示:

var sData = JSON.stringify({model: alumnes});

$.ajax({
     url: '@Url.Action("insertarfaltas","Faltas")',
     type: 'POST',
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     traditional: true,
     data: sData,
     success: function (data) {
           console.log(data);
     }
});
$('#guardarfaltas').click(function() {
            var i = 0;
            var alumnes = [];
            var fechactual = SaberFechaActual();
            $('#tablaalumnos tr').each(function () {
                var id = $(this).find("td").eq(0).html();
                var checkbox = $(this).find("td").eq(2).html();
                var comentario = $(this).find("#comentario").val();

if ($(this).find("input[type=checkbox]").is(":checked")) {
               alumnes[i] = { 
               Id: id,
               Comentario: comentario
               Fechactual: fechactual,
               Hora: hora,
               Codiuf: codiuf,
               Codicurs: codicurs,
               Codimodul: codimodul
               };
                    i++;
                }
            });

            var sData = JSON.stringify({model: alumnes});
            console.log(sData);


            $.ajax({
                url: '@Url.Action("insertarfaltas","Faltas")',
                type: 'POST',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                traditional: true,
                data: sData,
                success: function (data) {
                    console.log(data);
                }
});
我会尝试将二维数组转换为@Coulton建议的ViewModel

[HttpPost]
public ActionResult AnotherAction(List<MyRequestModel> model)
{

}
然后使用我建议的JSON.stringify方法

[HttpPost]
public ActionResult AnotherAction(List<MyRequestModel> model)
{

}
并创建
ViewModel
@Coulton建议:
MyRequestModel

编辑 尝试将URL从JavaScript中取出:

var url = '@Url.Action("insertarfaltas","Faltas")'

       $.ajax({
            url: url,
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            traditional: true,
            data: sData,
            success: function (data) {
                console.log(data);
            }

}))

从外观上看,变量
alumnes
不是字符串数组,它是
array[array[string]]
是的,因为可能不止1个,所以我需要2[0]['whatever']['whatever2'][1]['whatever]['whatever][/whater']。我的问题是试图到达另一个页面,我可以稍后修复所有的小问题,我想,但首先我想用一个简单的数组到达那里,有什么例子吗?它必须是一个字符串数组,或者绑定到一个对象会更好,因为它更具描述性?我不在乎如何做,但我需要一个二维数组中的行的6个参数,并将其传递到另一个页面,然后获得该数组,也许一个带有此代码的简单数组的示例会很有用。我实际需要的是一个多维数组,保存在00,01,02,03中,那么另一个人将是10,11,12,12,另一个人,20,21,22,23,所以我需要一个多维数组传递到我的函数,我不能使用简单数组,因为这会使我对我得到的每个数组都做一些处理,并且会使页面变得非常缓慢。我对MVC来说是新的几天,我做过你的,但我不知道怎么做库尔顿在那里做的。仍然存在相同的问题:SyntaxError:提供给RegExp构造函数“insertarfaltas”的标志无效。您能看到调试时发生异常的行吗?{“alumnes”:[[“6285”,“04-16-2015”,“32857,131277”,“6298”,“04-16-2015”,“32857,131277]]Faltas:273未捕获的语法错误:提供给RegExp构造函数“Faltas”的标志无效。我最后做的是做一个简单的操作,输入一个带“”的字符串来分隔每个单词,然后在操作中将它们拆分为一个数组。谢谢大家的帮助。这肯定会减慢你的应用程序