1

Closed

Error converting value {null} to type 'System.Int32'.

description

I am using WebAPI with Kendoui Grid. My updates are working fine, but running into problem with Addnew.
The code is throwing the following error
Error converting value {null} to type 'System.Int32'. Path 'Country_ID'
during Debug,The values in the object are as follow
Country_ID=0
Country_Code="IN"
Country_Name="India"

Country_Desc="jhjk"

[HttpPost]
    public HttpResponseMessage AddCountry(Country country)
    {

        if (ModelState.IsValid)
        {
            db.Countries.Add(country);
            db.SaveChanges();

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, country);
            response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = country.Country_ID }));
            return response;
        }
        else
        {
            string messages;
            try

            {
                //Extract Error
                messages= string.Join("; ", ModelState.Values
                                    .SelectMany(x => x.Errors)
                                    .Select(x => x.ErrorMessage));
                if (messages.Trim() == "")
                {
                    messages = string.Join("; ", ModelState.Values
                                        .SelectMany(x => x.Errors)
                                        .Select(x => x.Exception.Message));
                }



            }
            catch (Exception)
            {

                throw;
            }
            return Request.CreateResponse(HttpStatusCode.BadRequest,"Model is Invalid : " + messages);
        }
    }
Closed Dec 30, 2012 at 7:19 AM by JamesNK
Not a bug.

comments