0

I am trying to send data from my Javascript to my post method controller. I have knockout as my method to bind from Javascript to my view.

This is my controller, and this is wher convocationData is null

public async Task<ActionResult> CreateConvocation(string convocationData)
{
    string baseUrl = "http://localhost:8080";
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(baseUrl);

This is how I try to call the method using jQuery:

var tmpSession = self.addSession();
var tmpDescription = self.addDesc();
var convocationData = {
            sessionCode: tmpSession,
            description: tmpDescription
        };

$.ajax({
            url: '/Convocations/CreateConvocation',
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(convocationData),
            contentType: 'application/json',
            success: function (data) {
                self.Convocations.push(new Convocation(self.addSession(), self.addDesc()));
                self.addDesc("");
                self.addSession("");
            },
            error: function (err) {
                alert(err.status + " : " + err.statusText);
            }
        });
2
  • 2
    you are passing 2 params from javascript object convocationData (one called "sessionCode" and another called "description"), but your controller action is only accepting a 1 param named "convocationData". Your service-side action doesn't know or care that you called your javascript object varaiable... just its properties and values. Also, I don't think you need to JSON.stringify your data object. Commented Jun 30, 2020 at 20:25
  • 2
    Also, I would decorate your controller action with [HttpPost] and instead of adding the parameters 1 by 1 to the action method, create a model that matches the object your are posting from javascript (property names and data types). So your action method would look like this maybe: public async Task<ActionResult> CreateConvocation(ConvocationData model). Then just create that POCO class (ConvocationData) in C# with the correct properties Commented Jun 30, 2020 at 20:30

1 Answer 1

2

Add [FromBody] attribute like this

[HttpPost]
public async Task<ActionResult> CreateConvocation([FromBody]string convocationData)
{
    return text;
}

You can read more here Accepting Raw Request Body Content with ASP.NET Web API

Update:

Also, read zgood's comments to your post. Instead of using JSON.stringify you should create a corresponding model as he is describing in his comment.

Update 2:

JS model class

class Convocation {
  constructor(sessionCode, description) {
    this.sessionCode = sessionCode;
    this.description = description;
  }
}

C# DTO class

public class Convocation
{
    public string SessionCode{ get; set; }
    public string Description { get; set; }
}

Then you can update your action method to this

[HttpPost]
public async Task<ActionResult> CreateConvocation([FromBody]Convocation convocation)
{
    return text;
}

and ajax post to this:

$.ajax({
    url: '/Convocations/CreateConvocation',
    type: 'POST',
    dataType: 'json',
    data: new Convocation(tmpSession, tmpDescription),
    contentType: 'application/json',
    success: function(data) {
        self.Convocations.push(new Convocation(self.addSession(), self.addDesc()));
        self.addDesc("");
        self.addSession("");
    },
    error: function(err) {
        alert(err.status + " : " + err.statusText);
    }
});

I didn't test the code but it should work.

Sign up to request clarification or add additional context in comments.

2 Comments

Would it be possible to show how to call that model in javascript. I notice only one element get set
I notice your method only send one arguments, basically I need a class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.