首页 > 解决方案 > 从主控制器上的 Jquery/Ajax 检索地图功能数据

问题描述

在我的 .cshtml 上,我在容器内获取了用户以这种方式选择的所有按钮:

<script>

    $("#createnewidx").click(function (e) {

        console.log('here111');

        var selected = $("#WholeTagBlock input:checked").map(function (i, el) { return el.name; }).get();
        alert("selected = [" + selected + "]\nas string = \"" + selected.join(";") + "\"");

        $.ajax({
            url: '@Url.Action("CreateNewIdx")',
            data: selected.serializeArray(),
            type: 'POST',
        });

        console.log('here333');
    });

</script>

在我的“选择”上,我能够获得用户选择的所有按钮。现在,我需要将此信息传递给我的控制器。有了这个,我发现最直接的想法可能是使用 Jquery Post(例如,在这里尝试这样的想法)。

我的主要目标是将此数据发送到“公共 ActionResult CreateNewIdx”,这是我将用于将此数据插入数据库的函数。

我也尝试使用这个想法:

    $.ajax({
        type: 'POST',
        url: '@Url.Action("CreateNewIdx")',
        data: { 'email': 'che607@yahoo.com' },
        dataType: 'json',
        cache: false,
        contentType: "application/jsonrequest; charset=utf-8",
        success: function (data) {
            console.log('SUCCESS: ', data);
        },
        error: function (data) {
            console.log('ERROR: ', data);
        },
    });

我的问题是,当我尝试在我的操作上检索数据时,它总是为空。我在检索时的想法是:

public ActionResult CreateNewIdx(String email) (来自上面的例子) public ActionResult CreateNewIdx(String FixedTag) (或“email2”,下一个例子):

    $.ajax({
        url: '@Url.Action("CreateNewIndex")',
        type: "POST",
        dataType: 'json',
        data: { 'email2': selected},
        cache: false,
        contentType: "application/jsonrequest; charset=utf-8",
        success: function (data) {
            alert("hi" + data);
            console.log('SUCCESS2: ', data);
        }
    });

按照一条评论的想法,我尝试了:

     $.ajax({
        type: 'POST',
        url: '@Url.Action("CreateNewIndex")', // the method we are calling
        data: JSON.stringify({ 'person' : 'che607@yahoo.com' }),
        success: function (data) {
            //alert('Yay! It worked!');
            console.log('SUCCESS: ', data);
        },
        error: function (data) {
            //alert('Oh no :(');
            console.log('ERROR: ', data);
        }
    });

最后,我尝试了:

     $.ajax({
        type: 'POST',
        url: '@Url.Action("CreateNewIndex")', // the method we are calling
        data: JSON.stringify({ person: selected }),
        success: function (data) {
            //alert('Yay! It worked!');
            console.log('SUCCESS: ', data);
        },
        error: function (data) {
            //alert('Oh no :(');
            console.log('ERROR: ', data);
        }
    });

添加内容类型:“应用程序/json”:

        $.ajax({
        type: 'POST',
        url: '@Url.Action("CreateNewIndex")', // the method we are calling
        contentType: "application/json",
        data: JSON.stringify({ 'person4': 'testing11'}),
        success: function (data) {
            //alert('Yay! It worked!');
            console.log('SUCCESS: ', data);
        },
        error: function (data) {
            //alert('Oh no :(');
            console.log('ERROR: ', data);
        }
        });

并且:

$.ajax({
        type: 'POST',
        url: '@Url.Action("CreateNewIndex")', // the method we are calling
        contentType: "application/json",
        data: JSON.stringify({ person3: selected}),
        success: function (data) {
            //alert('Yay! It worked!');
            console.log('SUCCESS: ', data);
        },
        error: function (data) {
            //alert('Oh no :(');
            console.log('ERROR: ', data);
        }
    });

我错过了什么吗?

标签: jqueryasp.netrazormodel-view-controller

解决方案


推荐阅读