Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC JsonResult has data, but browser has none

I have the controller method GetLayer2(). It is extremely similar to GetLayer0() and GetLayer1()

[HttpGet]
public async Task<JsonResult> GetLayer2(string datePassedIn, string eventType, string driverId)
{
    string orgCode = "HVO"; //User.Identity.GetOrgCode();
    DateTime? processDate;
    DateTime defaultDate = DateTime.Today.AddDays(-1);  //default yesterday 

    if (String.IsNullOrEmpty(datePassedIn))
    {
        _logger.LogError(String.Format("Date passed in was NULL or empty. Using default date {0}", defaultDate.ToString(inputDateFormat)), null);
        processDate = defaultDate;
    }
    else
    {
        try
        {
            processDate = DateTime.ParseExact(datePassedIn, inputDateFormat, cultureProvider);
        }
        catch (FormatException ex)
        {
            _logger.LogError(ex, "Error formatting date {datePassedIn} did not match {inputDateFormat}. using default date {defaultDate}", null);
            processDate = defaultDate;
        }
    }

    driverId = HttpUtility.UrlDecode(driverId);


    IEnumerable<EventTypeLayer2> results = await _context.EventTypeLayer2Results
                                                        .FromSql($"usp_dd_EventType_2 @p0, @p1, @p2, @p3", orgCode, processDate, eventType, driverId)
                                                        .ToListAsync();


    JsonResult jr = Json(results);
    return jr;
}

The code can be summed up as:

  • Get the OrgCode
  • Get and parse the datePassedIn to the processDate
  • UrlDecode the driverId
  • Execute a stored proc, putting the output in results
  • Convert results to Json (This was added to aid in troubleshooting)
  • return the converted results

Originally it was return Json(results); When debugging the jr value has the correct Value results

Image of paused VS session showing value of <code>jr</code>

When viewed in the browser (Chrome) in the network tab, I get the correct count of objects, but they are all empty.

Image of results in Network tab

How do I get my results?

Edit - added class definition. Although these are EF classes, they are being filled by stored procedures.

[DataContract]
public class EventTypeLayer2
{
    [IgnoreDataMember]
    [Key]
    public Int64 RowId { get; set; }

    [Column(TypeName = "varchar(50)")]
    public string EventTypeDisplay { get; set; }

    [Column(TypeName = "varchar(50)")]
    public string EventTypeId { get; set; }

    [Column(TypeName = "varchar(20)")]
    public string Colour { get; set; }

    [Required]
    public int Value { get; set; }

    [Column(TypeName = "varchar(100)")]
    public string DriverId { get; set; }

    public int NodeId { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime StartTime { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime EndTime { get; set; }

    public int FirstLogId { get; set; }

    public int LastLogId { get; set; }

    public int MinSpeed { get; set; }

    public int MaxSpeed { get; set; }

    public int AvgSpeed { get; set; }

    public int CalcSummOdo { get; set; }
}
like image 508
Hecatonchires Avatar asked Sep 02 '19 23:09

Hecatonchires


Video Answer


1 Answers

You are using [DataContract] attribute for EventTypeLayer2 class, but you did not provide [DataMember] attribute for any of the class properties.

From documentation:

You must also apply the DataMemberAttribute to any field, property, or event that holds values you want to serialize.

This is the reason you are getting the right count of the objects, but without any valid member.

Possible solutions:

1) Remove the [DataContract] attribute from the class. By default, all public properties will be serialized.

2) Add [DataMember] attribute to properties you wish to serialize.

like image 67
EylM Avatar answered Oct 06 '22 22:10

EylM