I have got this javascript array:

formatData = {
                            title: title,
                            start: startFormat,
                            end: endFormat
                        };

I would like to send the formatData to a method of the C# controller(and receive it as an array to post to the database):

//HomeController.cs
public ActionResult setReservation()
    {
        Database db = new Database();
        db.setReservations(reservation);
        return View();
    }

i have tried to send the javascript array with the following AJAX code:

$.ajax({
                            url: '/Controller/HomeController',
                            type: 'POST',
                            contentType: 'application/json',
                            data: JSON.stringify({
                                formatData: formatData
                            }),

                        });

with no success. The browser "network" tab shows 404. What am I doing wrong? How can I catch the array from client-side javascript in the server side c# controller as a C# array-like? How could I attach the AJAX post to the setReservation method in the c# controller?

Many thanks in advance.

Edit: formatData should pass to the Controller(HomeController.cs). In the controller, the data inside formData(title, start, end) must be passed to a method(setReservations) of an object(db) of another class(Database), where the data will be injected in a sql query string.

  • MVC Route dont consider Controller word while making the request additionally you need to pass Action Method or else use Route Attribute – Debashish Saha yesterday
  • Moreover handle the exception in the Ajax call.Else application may ran into trouble if the server exception occours – Debashish Saha yesterday

First of all, the URL from your AJAX method is wrong. Try something like this:

$.ajax({
                        url: '/Home/SetReservation',
                        type: 'POST',
                        contentType: 'application/json',
                        data: JSON.stringify({
                            formatData: formatData
                        }),

                    });

Then, in the controller, you need to have the list as a parameter for the action you want to use. Also, mark the action as [HttpPost]:

[HttpPost]
public ActionResult SetReservation([FromBody]Reservation reservation)
    {
        Database db = new Database();
        db.setReservation(reservation);
        return View(); // you might want to redirect to another view instead
    }
  • thank you for your answer. Could you please explain why you use a List<Reservation> reservations as parameter? Shouldn't it be List<string>, because i am posting just a JSON string? – Tio yesterday
  • Sorry, that was my mistake. I was confused by you saying that you want to receive an array. The JSON object that you send will be read as a C# object, if possible. So you can use a Model class instead of a list of strings. If you them in another format, maybe you should provide more details. – Mircea Oprea yesterday
  • I'm sorry, my fault. I've edited the question with an explanation of the steps it should take. I hope it clarifies. – Tio yesterday

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.