0

On my .cshtml, I am getting all the buttons, inside a container, that were selected by the user this way:

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

On my "selected" I am being able to get all the buttons that were selected by the user. Now, I need to pass this information to my controller. With that, I found that likely the most straight forward idea would be using a Jquery Post (For example, trying ideas like this one here).

My main goal is to send this data to a "public ActionResult CreateNewIdx" which is the function that I will be using to insert this data into a DB.

I also tried to use this idea:

    $.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);
        },
    });

My problem is that when I try to retrieve the data on my Action it is always coming as null. The way that I was thinking in retrieving is:

public ActionResult CreateNewIdx(String email) (from the above example) public ActionResult CreateNewIdx(String FixedTag) (or "email2", for this next example):

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

Following the idea of one comment, I tried:

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

Finally, I tried:

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

Adding contentType: "application/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);
        }
        });

And also:

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

Am I missing something?

FFLS
  • 565
  • 1
  • 4
  • 19
  • Content type is wrong and you aren't sending json. You have to stringify data yourself – charlietfl Jul 16 '19 at 16:42
  • @charlietfl, I had tried this before but it also did not work. I just updated my question with the 2 examples that I tried. – FFLS Jul 16 '19 at 17:43
  • OK but you need `application/json` contentType to send json. You had something like `application/jsonrequest` which I have never heard of – charlietfl Jul 16 '19 at 17:46
  • @charlietfl, I updated with the couple attempts that I did. – FFLS Jul 16 '19 at 18:17

0 Answers0