© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
11 replies
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));
// 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; }
        }
// 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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ ✅ Pass A Function To Another Function?
C#CC# / help
3y ago
❔ How to make instantiated object move using transform function?
C#CC# / help
4y ago
❔ How to make instantiated object move with transform function?
C#CC# / help
4y ago
❔ How to pass cookies from .NET application to Next.js app?
C#CC# / help
3y ago