MVC .net 上使用 Ajax 调用 Controller 函数的 Javascript 函数不起作用

标签 javascript jquery ajax regex asp.net-mvc

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

我得到的唯一输出是 Uncaught

SyntaxError: Invalid flags supplied to RegExp constructor 'insertarfaltas'.

怎么了?我只想将一个数组传递给名为 insertarfaltas 的 Controller 函数。

 $('#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);
                }
            });

---- Controller 文件夹:

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

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


        }

最佳答案

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

但是,我很想更改对对象的绑定(bind),因为这在代码中更具描述性。

行动

public ActionResult AnotherAction(List<MyRequestModel> model)
{

}

绑定(bind)模型

public class MyRequestModel
{
    public int Id { get; set; }
    public string Commentario { get; set; }
    public string Fechactual { get; set; }
    public string Hora { get; set; }
    public string Codiuf { get; set; }
    public string Codicurs { get; set; }
    public string Codimodul { get; set; }
}

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

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

关于MVC .net 上使用 Ajax 调用 Controller 函数的 Javascript 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29678198/

相关文章:

javascript - jQuery - 单击更改父级和其他 div

php - 总结 2 个在 PHP 中动态创建的值

javascript - 自动隐藏 tabbable 内的 "Load more"按钮

php - 这是关于 Jquery/JS 的,如果我更改元素的 HTML - 我是否能够对其执行其他 Jquery/JS 操作

javascript - 从 JSON 响应中删除 '@' 作为 String() 后,在 WSO2 序列中使用 setPeyloadJSON() 时收到错误

javascript - 这是有效的 JSON 吗?

javascript - Django 动态表单集不工作

jquery - 使用 Javascript/Jquery 和 Json 数据填充选择框选项

php - Admin-ajax.php 302 错误 WordPress

javascript - 自动计算输入字段