locked
How to do dynamic Json data Model binding in WebAPI ? RRS feed

  • Question

  • User-768377618 posted

    Hi!

    In WebAPI I am passing any number of JSON string and want to bind only specific key value pairs like Phone number, First Name etc.

    I have already tried dynamic parameter but getting null.

    Below code works only when the JSON string has same number of values in the model class.

    Here is the Code:

        

        [HttpPost]
        public string PushLead(LeadInsertParam pram)
            {
                
                if (pram.Phone == "")
                {
    
                    Result = "Required parameter not found: Phone= " + pram.Phone;
                    Writelog.WriteError("Error: " + Result);
                    resp.ResultStatus = "Error";
                    resp.ResultDescription = Result;
    
                }
                else
                {
                string Phone= pram.Phone;
                                
                    
                }
              
                //   return Result;
              
                Result = JsonConvert.SerializeObject(resp);
                return Result;
            }  
    



    In Model the class LeadInsertParam  is

         

      public class LeadInsertParam
         {
        [Key]
        public string Phone { get; set; }
        public string ProspectID { get; set; }
        public string FirstName { get; set; }
        public string mx_Course { get; set; }
        public string EmailAddress { get; set; }
        public string mx_Location { get; set; }
       
            }





    The Json String can be :

    {"ProspectID":"b67273-4735m","FirstName":Mike,"LastName":null,"EmailAddress":"abc@3ee13.com","Origin":"API","Phone":null,"Mobile":"8888888888","Source":null,"SourceMedium":null,"SourceCampaign":null,"DoNotEmail":"0","ProspectStage":"Prospect","Score":"0","CreatedByName":"Subham B","ModifiedByName":"Subham B","CanUpdate":"true"}





    Saturday, December 31, 2016 5:17 AM

All replies