Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm quite new to Web-Api concepts. Trying to insert data using the basic project provided by Microsoft (Adding a simple Test Client to ASP.NET Web API Help Page).

http://blogs.msdn.com/b/yaohuang1/archive/2012/12/02/adding-a-simple-test-client-to-asp-net-web-api-help-page.aspx

Below is the code for Inserting data into Headoffice table.(HeadofficeID int ,HeadofficeName varchar(50),Notes varchar(1000),Isactive bit)

C#
[POST("create")]
        public HttpResponseMessage Post([FromUri]HeadOfficeModel headOffices)
        {
            if (_headOfficeBLL.Insert(headOffices) > 0)
                return Request.CreateResponse(HttpStatusCode.Created, headOffices);
            else
                return Request.CreateResponse(HttpStatusCode.BadRequest, headOffices);
        }


Headofficemodel Class

C#
public partial class HeadOfficeModel : AtlAuthenticationModelBase
    {
        public int HeadOfficeId { get; set; }
        public string HeadOfficeName { get; set; }
        public string Notes { get; set; }
        public bool IsActive { get; set; }
    }


In the front end when i try to send the data from URI or Body only null values are getting inserting. While debugging all i can see in Headoffice model is null values.Below are the different ways i tried to insert data

C#
1) {"HeadOfficeName":"TestHO1", "Notes":"notes", "IsActive":true}
2) {"TestHO1", "notes", true}
3) ={"headOffices":{"HeadOfficeName":"TestHO1","Notes":"notes","IsActive":false}}


and also tried to change the code as below

C#
public HttpResponseMessage Post([FromUri]HeadOfficeModel headOffices)
public HttpResponseMessage Post([FromBody]HeadOfficeModel headOffices)
public HttpResponseMessage Post([ModelBinder]HeadOfficeModel headOffices)


Been trying to fix this from two days. When i send the data as complex type its not working else as separate parameters (changing the method to accept parameters) its working fine

C#
public int Post(string Name, string Notes, bool Active)
    {
        HeadOfficeModel objHOM = new HeadOfficeModel();
        objHOM.HeadofficeName = Name;
        objHOM.Notes = Notes;
        objHOM.IsActive = Active;
        return _headOfficeBLL.Insert(objHOM);
    }


Below is the html code where i m hiting while inserting

C#
<script>
  testClientModel = {
    HttpMethod: '@Model.ApiDescription.HttpMethod',
    UriPathTemplate: @Html.Raw(Json.Encode(Model.ApiDescription.RelativePath)),
    UriParameters: [
            @foreach (var parameter in Model.ApiDescription.ParameterDescriptions)
            {
              if (parameter.Source == System.Web.Http.Description.ApiParameterSource.FromUri)
              {
                    @:{ name: "@parameter.Name", value: "" },
                }
            }
    ],
    Samples: {
      @Html.Raw(@String.Join(",", Model.SampleRequests.Select(s => String.Format("\"{0}\": \"{1}\"", s.Key, HttpUtility.UrlEncode(s.Value.ToString()))).ToArray()))
    },
    BaseAddress: '@applicationPath'
  };
</script>


Can you please help me where am i going wrong. Attaching screenshot.

Entered both in URI and Body just to show that i tried different ways.

Regards,
Prathap.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900