locked
pass object to ASP.NET MVC WebApi RRS feed

  • Question

  • User-1243688316 posted

    Hi every oneSmile

    I try to pass a class object to method like this:

    public bool Add(tbl_years year)
    {
    YearRepository bl = new YearRepository();
    if (ModelState.IsValid)
    {
    if (year.yearStart != null & year.yearEnd != null)
    {
    year.yearStart = year.yearStart.Value.ToMiladiDate();
    year.yearEnd = year.yearEnd.Value.ToMiladiDate();
    }
    if (bl.Add(year))
    {

    return true;
    }
    else
    {
    return false;
    }
    }

    return false;
    }

    Now,How should i write the url?
    should i write the all property of tbl_years?

    Thanks

    Wednesday, August 23, 2017 6:17 AM

Answers

  • User-271186128 posted

    Hi arfaie,

    arfaie

    Now,How should i write the url?

    The api url like this: http://localhost:64186/api/Test/Add

    arfaie

    should i write the all property of tbl_years?

    No, just write the required property. please refer to the following sample code:

    Code in Model:

    public class tbl_years
        {
            public int Id { get; set; }
            public DateTime yearStart { get; set; }
            public DateTime yearEnd { get; set; }
            public string Name { get; set; }
        }
    

    Code in api Controller:

    public class TestController : ApiController
        {
           [HttpPost]
            public bool Add(tbl_years year)
            { //set a break point to check the value.
                var start = year.yearStart;
                var end = year.yearEnd;
                if (start != null && end != null)
                {
                    return true;
                }
                return false;
            }
        }
    

    Code in View:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script src="Scripts/jquery-1.10.2.min.js"></script>
        <script>
            function PassYear() {
                var tbl_years = {
                    Id: "1",
                    yearStart: "2017-8-23",
                    yearEnd: "2017-8-29" //,
                    //Name: "Peter" // if the Name property is not required, you could ignore
                };
                $.ajax({
                    type: "POST",
                    url: 'http://localhost:64186/api/Test/Add',//change to your url
                    data: JSON.stringify(tbl_years),// pass json object to web api
                    contentType: 'application/json',
                    success: function () {
                        alert('success!');
                    }
                });
            }
    
        </script>
    </head>
    <body>
        <input type="button" value="PassYear" onclick="PassYear()" />
    </body>
    </html>
    
    

    Then, when you debug your code from the controller, you can see the Name property is null.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 23, 2017 11:17 AM

All replies

  • User-271186128 posted

    Hi arfaie,

    arfaie

    Now,How should i write the url?

    The api url like this: http://localhost:64186/api/Test/Add

    arfaie

    should i write the all property of tbl_years?

    No, just write the required property. please refer to the following sample code:

    Code in Model:

    public class tbl_years
        {
            public int Id { get; set; }
            public DateTime yearStart { get; set; }
            public DateTime yearEnd { get; set; }
            public string Name { get; set; }
        }
    

    Code in api Controller:

    public class TestController : ApiController
        {
           [HttpPost]
            public bool Add(tbl_years year)
            { //set a break point to check the value.
                var start = year.yearStart;
                var end = year.yearEnd;
                if (start != null && end != null)
                {
                    return true;
                }
                return false;
            }
        }
    

    Code in View:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script src="Scripts/jquery-1.10.2.min.js"></script>
        <script>
            function PassYear() {
                var tbl_years = {
                    Id: "1",
                    yearStart: "2017-8-23",
                    yearEnd: "2017-8-29" //,
                    //Name: "Peter" // if the Name property is not required, you could ignore
                };
                $.ajax({
                    type: "POST",
                    url: 'http://localhost:64186/api/Test/Add',//change to your url
                    data: JSON.stringify(tbl_years),// pass json object to web api
                    contentType: 'application/json',
                    success: function () {
                        alert('success!');
                    }
                });
            }
    
        </script>
    </head>
    <body>
        <input type="button" value="PassYear" onclick="PassYear()" />
    </body>
    </html>
    
    

    Then, when you debug your code from the controller, you can see the Name property is null.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 23, 2017 11:17 AM
  • User-1243688316 posted

    Special Thanks :)

    Thursday, August 24, 2017 8:08 AM