1

I would like to pass array values from javascript to my C#.

Currently I am getting GUID null in C#. Not sure where I am doing wrong.

When I check developer tool, I have values

http://localhost/mvc/CheckData?GUID[]=C71F952E-ED74-4138-8061-4B50B9EF6463&ColumnVal=1&RowVal=1

I would like to receive this GUID value in my C# code.

JavaScript

function CheckData(obj) {
    $.ajax({
        data: {GUID:eArray, ColumnVal: $('#DisColumn').val(), RowVal: $('#DisRow').val()},
        url: "/mvc/CheckData",
                cache: false,
        success: function (result) {
         ....
        }
    });
}

C# backend code to receive values from front-end.

        public ActionResult CheckData()
        {
             var GUID = HttpContext.Request["GUID"];
            int columnVal = Convert.ToInt32(HttpContext.Request["ColumnVal"]);
            int rowVal = Convert.ToInt32(HttpContext.Request["RowVal"]);
    
            string result = (Services.CheckDataRecords(rowVal, columnVal,GUID)) ? "true" : "false";

            return Content(result);
        }

Currently, I am getting null when it hits to C# method var GUID = HttpContext.Request["GUID"];. I can see array value in front-end. But it is somehow not passing that value.

0

1 Answer 1

1

HttpContext.Request represents the request, and to access query data, you will need to do something like this: HttpContext.Request.Query["GUID"].

A more straightforward approach is to let ASP.NET do the work for you is just turn your C# backend to this:

[HttpGet("CheckData")] //change the route based on your code
public ActionResult CheckData([FromQuery] Guid[] guid, int columnVal, int rowVal)
{
    var GUIDs = guid;
    int column = columnVal;
    int row = rowVal;

    .....//your code
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.