Elaman Aitymbet
Elaman Aitymbet

Reputation: 515

Web API server controller doesn't get parametrs

I send JSON data from android app. In server side I have issue - Web API server controller doesn't get parametrs. From client side I send json data like this:

{   "TypeID ": "1",
    "FullName": "Alex",
    "Title": "AlexTitle",
    "RegionID": "1",
    "CityID ": "1",
    "Phone1": "+7(705)105-78-70"
}

Any help appreciated, please if you have some information, idea let me know, thank you! This is controller

 [System.Web.Http.HttpPost]
    public HttpResponseMessage PostRequisition([FromBody]string requisition)
    {
        Requisition postReq = new Requisition();
       if (!String.IsNullOrEmpty(requisition))
        {
            dynamic arr = JValue.Parse(requisition);
            //PostReq model = JsonConvert.DeserializeObject<PostReq>(requisition);
            postReq.FullName = arr.FullName;
            postReq.CityID = Convert.ToInt32(arr.CityID);
            postReq.RegionID = Convert.ToInt32(arr.RegionID);
            postReq.TypeID = Convert.ToInt32(arr.TypeID);
            postReq.UserID = 8;
            postReq.Title = arr.Title;
            postReq.Date = Convert.ToDateTime(arr.Date, CultureInfo.CurrentCulture);
            postReq.Decription = arr.Description;
            postReq.Phone1 = arr.Phone1;
            postReq.Activate = false;
            postReq.ClickCount = 0;
            try
            {
                db.Requisition.Add(postReq);
                db.SaveChanges();
                Message msg = new Message();
                msg.Date = DateTime.Now;
                msg.Type = "POST";
                msg.Text = "OK";
                db.Message.Add(msg);
                db.SaveChanges();
                return Request.CreateResponse(HttpStatusCode.OK, postReq);
            }
            catch (Exception ex)
            {
                Message msg = new Message();
                msg.Date = DateTime.Now;
                msg.Type = "POST";
                msg.Text = "ERROR";
                db.Message.Add(msg);
                db.SaveChanges();
                return Request.CreateResponse(HttpStatusCode.OK, ex.Message);
            }
        }
        else
        {
            Message msg = new Message();
            msg.Date = DateTime.Now;
            msg.Type = "POST";
            msg.Text = "null";
            db.Message.Add(msg);
            db.SaveChanges();
            return Request.CreateResponse(HttpStatusCode.OK, "null");
        }

    }

Upvotes: 1

Views: 55

Answers (1)

jpgrassi
jpgrassi

Reputation: 5762

Your problem is simple. You are sending a JSON object but are expecting a string in your POST action. Simple way to fix this is creating a class that maps to your JSON object:

public class RequisitionViewModel
{
    public int TypeID {get; set;}
    public string FullName {get; set;}
    public string Title {get; set;}
    public int RegionID {get; set;}
    public int CityID  {get; set;}
    public string Phone1 {get; set;}    
}

Then, change your action signature to:

[FromBody]RequisitionViewModel requisition)

You also don't need all the converting in your code:

postReq.FullName = requisition.FullName;
postReq.CityID = requisition.CityID;
//other fields...

Upvotes: 3

Related Questions