C#C
C#4y ago
James213

❔ How to pass JS object to function.

How do I pass the JS object to the C# function in that it accepts it as an object of a defined Class?
I looked at an example using Ajax (don't know if that has to do with anything).
https://www.dotnettricks.com/learn/webapi/how-to-pass-javascript-complex-object-to-aspnet-web-api-and-mvc

I'm getting null values.

I tried removing JSON.stringify, but no luck.

// JS Object
const formData = { "startDate": startDateInput.value, "endDate": endDateInput.value, "stock": stock.value };

// Send to C# function
const response = await fetch('https://localhost:5681/api/stock/getStockData/' + JSON.stringify(formData));


// C# function
[HttpGet]
[Route("api/stock/getStockData/{formData}")]
public async Task<IActionResult> GetStockDataAsync(Stock formData)
{

}

// Stock Class
        public class Stock
        {
            public string stock { get; set; }
            public string? startDate { get; set; }
            public string? endDate { get; set; }
        }


Thank you
Was this page helpful?