1

I have Web api controller:

[HttpPost]
public void SetAt(int id,TableModel idModel)
{
    table[id] = idModel;
}

I try to access it with the aid of the method

$http.post("http://localhost:58652/api/Values/SetAt", data);

But I do not know how to pass data to the method $http.post works correctly.

Data:

  • For id =0;
  • For idModel={ field: "Id", width: 200 };

How to do it?

2
  • I think, you want to pass multiple object. then try {{ "field": "0", "width": 200, }, { "field": "1", "width": 300, } } Commented Nov 20, 2014 at 14:15
  • I want to pass something like { id:0, idModel: { field: "Id", width: 200 }; } Commented Nov 20, 2014 at 14:21

1 Answer 1

2

This should work:

var data = {
        id: 0,
        idModel: {
            field: "id",
            width: 200
        }
    };

$http.post("http://localhost:58652/api/Values/SetAt", data);

Basically, the properties in your JavaScript object should match the parameters of your C# method. So

  • "id" will map to "int id" (with a value of 0), and
  • "idModel" will map to "TableModel idModel" (with the properties "field" and "width" set to "id" and "200" respectively)

I have not worked with Web API Controllers much, you may have to pass the "id" in the route (depending on your setup), which would make your code look like this:

var id = 0,
    data = {
        field: "id",
        width: 200
    };

$http.post("http://localhost:58652/api/Values/SetAt/" + id, data);
Sign up to request clarification or add additional context in comments.

4 Comments

this not work because "int id" takes "int" ,and pass object {"id":0}
with the post can not be transferred "?id=0"
I'm having some trouble understanding your comments, @Denis. Did you try the second solution I added to my answer? I just updated it because I left out a "/" at the end of the URL.
@Denis So this solved your problem? That's great! I'm glad to hear it =)

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.