PetaPoco ajax 400 Bad Request on correct model type

29 views Asked by At

I have a C# application that requests data from SSMS using a controller and PetaPoco as the ORM. It is work inherited from another developer but until this point has proven not difficult to work with. It is used to deploy various charts that display the data, often based on a single expanding table.

I have had issues in the past where my ajax call to my controller returns an HTTP 400 if the PetaPoco model is mistyped. In the past there was such an issue where I mistakenly used string instead of DateTime in the class for a date type column in SSMS. Returning it to the correct type in the model corrected the problem.

However, my current issue is in the same vein but I can't seem to fix it. I've narrowed it down to a single int type in my class, but its corresponding column in SMSS is also typed as int. I've even built a new table, imported my data into the new table, built a new PetaPoco model and the problem still persists.

The PetaPoco class is along the lines of

public class PetaPocoModel 
{
    public int follow_up_complete { get; set; }
    public DateTime follow_up_date { get; set; }
    public DateTime follow_up_ts { get; set; }
    public int pulhypo_complete { get; set; }
    ...
    public int fu2_medical_record_complete { get; set; }
    public int fu2_laboratory_complete { get; set; }
}

The value fu2_laboratory_complete is the problem child.

And is passed into my controller like

[AllowAnonymous]
[HttpGet]
        public HttpResponseMessage GetData()
        {
            try
            {
                using (var dataContext = new Database("DatabaseName")) 
                {

                    var allNeeds = dataContext.Fetch<PetaPocoModel>("SELECT * FROM dbo.TableName"); 

                    JavaScriptSerializer jSerializer = new JavaScriptSerializer();

                    string jsonObject = jSerializer.Serialize(allNeeds).Replace("\"\\/Date(", "").Replace(")\\/\"", "");

                    HttpResponseMessage objResponse = new HttpResponseMessage(HttpStatusCode.OK);
                    objResponse.Content = new StringContent(jsonObject);
                    return objResponse;
                }
            }
            catch
            {
                return new HttpResponseMessage(HttpStatusCode.BadRequest);
            }
        }

This is later used in an ajax call in some javascript that builds the charts.

If I comment out fu2_laboratory_complete, my ajax call works fine and my charts populate with all the data sans that one specific value. If I leave it in my ajax call immediately returns a Bad Request error.

Frustratingly, the other ..._complete values are all the same type and are imported into the table the same way and all work fine. I can view the design of my table in SSMS and they all show as int.

Am I missing something or are there some tricks I could try? This is all tied up in a DotNetNuke module. I have a narrow understanding of PetaPoco but this seems straightforward.

1

There are 1 answers

0
Zodack On

I frankly do not know the underlying issue, still, but I have found a solution.

I tried PetaPoco's database model generator, Database.tt to generate its own model of the table. This still did not fix anything. I tried switching from database.Fetch to database.Query to no avail.

What fixed this for me was cutting down on the number of entries in my model. I doubt this is a PetaPoco limitation and is likely to do with DotNetNuke or some other underlying issue.

It seems that keeping my model at 30 or fewer columns returns the data I expect, and going over 30 columns causes the 400 error. Maybe this has to do with the size of data being fetched by the ajax request and DotNetNuke having some upper limit.