locked
Post to WebAPI RRS feed

  • Question

  • User-1821194478 posted

    Hello,

      I have to post a list of values to WebAPI but the challenge is I only want to post the values but not the keys through controller . Is there a way that WebApi can accept only values and assigns to the keys in the order it received. some thing in this format "1","test1","test2" for WebAPI

    [{"$id":"1","ID":7,"FNAME":"First","LNAME":"Last"}]

    . Also I am using Entityframe work in my WebAPI how can I get the relationship between the tables on the client side.

    Friday, April 17, 2015 4:12 PM

Answers

  • User1644755831 posted

    Hello vamsi025,

    vamsi025

    I have to post a list of values to WebAPI but the challenge is I only want to post the values but not the keys through controller . Is there a way that WebApi can accept only values and assigns to the keys in the order it received. some thing in this format "1","test1","test2" for WebAPI

    Here the keys refers to the parameter names when you post the values so your web api must have parameters with name ID, FNAME , LNAME if you don't pass the keys and values it will give errors. now if you just want to pass the values you could change the web api method to accept only a single array of string[] type. then you can post this values to the webapi.

    Please follow this example on how to do this. http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/

    1. you need to add [FromBody] tag in your webapi method. If you want Web API to automatically convert POST data to your [FromBody] input parameter, you have to limit yourself to only a single parameter. See Parameter Binding.

    2. then you just pass the array of string values instead the key value parameters.

    // POST api/values
    public string Post([FromBody]string value) {
      return value;
    }
    
    
    var values = ["1","7","First","Last"];
    $.ajax({
        dataType: "json",
        url: "/api/values",
        method: "POST",
        data: { '': values }
    });
    

    Hope this helps.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 20, 2015 1:58 AM