locked
How to post parameter from body [FromBody] and retrive objects in a <list> in WebAPI? RRS feed

  • Question

  • User411771660 posted

    How to post parameter to a body and get the object in a list.Now i am able to retrieve the data using this post method in my controller with parameter appended to the URI

    Accounts Controller

        [HttpPost]
        [ActionName("mini")]
        public List<MiniStatement> GetMiniStatement(string accountNumber)
        {
            var miniState = BusinessLayer.Api.AccountHolderApi.GetMiniStatement(accountNumber);
            return miniState.ToList();
        }

    But how do i pass the param [FromBody] and retrive the data in a List?

    MiniStatementCLass

     public  class MiniStatement
    {
        public string AccountNumber { get; set; }
        public string TranDate { get; set; }
        public string Trans { get; set; }
        public decimal Amount { get; set; }
    }

    GetMiniStatement Method in DB Layer

    public static List<MiniStatement> GetMiniStatement(string accountNumber)
        {
            List<MiniStatement> resultList = new List<MiniStatement>();
            using (var conn = new NpgsqlConnection("Server=localhost;UserId = postgres; " + "Password = pes; Database = pmc;"))
            {
                conn.Open();
                using (var command = new NpgsqlCommand("SELECT * FROM sms.dep_mini_statement(@AccountNumber);", conn))
                {
                    command.Parameters.AddWithValue("@AccountNumber", accountNumber);
    
                    using (var dr = command.ExecuteReader())
                    {
    
                        while (dr.Read())
                        {
                            var result = new MiniStatement
                            {
                                AccountNumber = accountNumber,
                                TranDate = (string)dr[0],
                                Trans = (string)dr[1],
                                Amount = (decimal)dr[2]
                            };
                            resultList.Add(result);
                        }
    
                    }
                }
    
            }
            return resultList;
    
        }

    And the Route

    config.Routes.MapHttpRoute("MobileBankingApi", "v1/{controller}/{action}");



    Monday, June 12, 2017 6:16 PM

All replies

  • User1967761114 posted

    Hi tarun02,

    According to your description, see the following code:

    (1) ApiController:

    public class TestController : ApiController
    {
        [HttpPost]
        [ActionName("mini")]
        public List<MiniStatement> GetMiniStatement([FromBody]string accountNumber)
        {
            //var miniState = BusinessLayer.Api.AccountHolderApi.GetMiniStatement(accountNumber);
            //I worte some data to simulate the result data
            var result = new List<MiniStatement>();
            result.Add(new MiniStatement()
            {
                AccountNumber = "001",
                Amount = 1,
                TranDate = "2017/01/01",
                Trans = "test1"
            });
            result.Add(new MiniStatement()
            {
                AccountNumber = "002",
               Amount = 2,
                TranDate = "2017/01/02",
                Trans = "test2"
            });
            return result;
        }
    }

    (2) Client invoke

    $.ajax({
        //you could modify the url to yourself url at here
        url: "http://localhost:61072/api/test/mini",
        type: "POST",
        //at here you also could use:
        //contentType: "application/json",
        //data: '"accountNumber"',
        data: { "": "accountNumber" },
        dataType:"json",
        success: function (result) {
            console.log(result);
            for (var i = 0; i < result.length; i++) {
                //do something at here by result[i]
            }
        }
    })

    Then, the result as below:

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Tuesday, June 13, 2017 3:37 AM