0

I have JavaScript function where I have an array and when I send that array to my C# controller, it should be in such way way that my controller should understand.

JavaScript function

function Check(obj) {
    var eArray = $('..').map(function () {
        return this.getAttribute("value");
    }).get();

    $.ajax({
        url: "/Order/Check",
        data: { GUID: JSON.stringify(eArray) }, 
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        
        });

My Controller

public ActionResult Check()
        {
            string guid = HttpContext.Request["GUID"];
            var result = //send the result
            return Json(result, JsonRequestBehavior.AllowGet);
        }

I would like to get an array in my controller.

  • https://stackoverflow.com/questions/9586585/convert-json-to-a-c-sharp-array – firatt_ Jan 25 '23 at 09:04
  • ^^ Specifically: https://stackoverflow.com/a/61727094/982149 – Fildor Jan 25 '23 at 09:17
  • Hi @firatt_, In developer tool, JavaScript function, I am getting eArray as `eArray = (2) ['D5FAF478-CF43-40E1-BE79-BB90147A3194','2E79B23E-D264-4901-A065-7E0B7032A5D8']` and in controller the value of guid is `["D5FAF478-CF43-40E1-BE79-BB90147A3194","2E79B23E-D264-4901-A065-7E0B7032A5D8"]`. I need this an array format in C#. Please help me with this. –  Jan 25 '23 at 09:40
  • @Fildor, this is how I am getting the result for JavaScript function in developer and in controller `eArray = (2) ['D5FAF478-CF43-40E1-BE79-BB90147A3194','2E79B23E-D264-4901-A065-7E0B7032A5D8']` and `guid is ["D5FAF478-CF43-40E1-BE79-BB90147A3194","2E79B23E-D264-4901-A065-7E0B7032A5D8"]` –  Jan 25 '23 at 09:42
  • @testtest if `guid` is ["D5FAF478-CF43-40E1-BE79-BB90147A3194","2E79B23E-D264-4901-A065-7E0B7032A5D8"] maybe just use `Guid.Parse`? – virouz98 Jan 25 '23 at 15:20

2 Answers2

0

I'm not really sure what you are trying to achieve. From what I saw in your comments, you are sending an array of GUIDs to your controller, but that results in it being send as a string, and you want an array.

I tested your code and modified it a bit:

$.ajax({
    type: "POST",
    url: /your url/,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    cache: false,
    data: JSON.stringify({GUID: eArray}),
});

Where eArray is let eArray = ['D5FAF478-CF43-40E1-BE79-BB90147A3194', '2E79B23E-D264-4901-A065-7E0B7032A5D8']

Then, in my controller, I receive it as a model:

public class Dto
{
    public string[] GUID { get; set; }
}

Then, you can use it like this:

[HttpPost]
public IActionResult Post([FromBody] Dto dto)
{
    var listOfGuids = dto.GUID.Select(guid => Guid.Parse(guid)).ToList();
    var result = Services.CheckRecords(listOfGuids);
    ...
}
virouz98
  • 96
  • 8
-2

It seems that unfortunately the standard JavaScriptSerializer.Deserialize doesn't handle Guid type.

Therefore, I would go with something like

    public ActionResult Check()
    {
        string guidsStr = HttpContext.Request["GUID"];

        var guids = new List<Guid>();
        foreach (var guid in Regex.Replace(guidsStr, "[\\[\\\"\\]]", "").Split(",")) {
            Guid newGuid;
            if (Guid.TryParse(guid, out newGuid)) {
                guids.Add(newGuid);
            } else {
                // handle invalid guide value
            }
        }
        // guids list now contains parsed Guid objects
        // do here what you need to do with the list of guids

        return Json(result, JsonRequestBehavior.AllowGet);
    }

Please let me know if this helps.

RAllen
  • 1,235
  • 1
  • 7
  • 10
  • Hi Rallen, I am looking for a way to use returned JSON.stringify array in C# controller. I believe I need to store those values either in list or an array. –  Jan 25 '23 at 11:29
  • @testtest I've updated the answer. Please check if it looks closer to what you need. I'm not sure what you need to do before `return Json(result, JsonRequestBehavior.AllowGet);` though. Let me know what is needed there. – RAllen Jan 25 '23 at 11:37
  • Hi @Rallen, I have added one more line there in my controller. Could you please change your answer accordingly. –  Jan 25 '23 at 11:45
  • @testtest, I don't have information about what this new line `var result = Services.CheckRecords(guid);` does, what it accepts as an argument and what it returns in the result. Without this understanding, I cannot integrate my solution with it. – RAllen Jan 25 '23 at 15:01