0

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.

2
  • MVC Route dont consider Controller word while making the request additionally you need to pass Action Method or else use Route Attribute Commented Jun 3, 2018 at 20:13
  • Moreover handle the exception in the Ajax call.Else application may ran into trouble if the server exception occours Commented Jun 3, 2018 at 20:15

1 Answer 1

1

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
    }
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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.
I'm sorry, my fault. I've edited the question with an explanation of the steps it should take. I hope it clarifies.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.