hughesdan
hughesdan

Reputation: 2851

My JsonResult contains values without field names

The problem here is pretty self-evident from the resulting JSON array at the bottom. From my code I can't determine why the output doesn't contain the field names (e.g. "dma":"Albany-Schenectady-Troy" rather than just "Albany-Schenectady-Troy"). Can someone spot what the issue might be?

Model

    public IEnumerable<EstimateDetailsModel> GetEstimateDetails(int id)
    {
        var estimateDetails = from e in db.Estimates
                              join es in db.EstimateStations on e.EstimateID equals es.EstimateID
                              join s in db.Stations on es.StationID equals s.StationID
                              join m in db.Markets on s.MarketID equals m.MarketID
                              where e.EstimateID == 1
                              select new EstimateDetailsModel { Dma = m.DmaName, CallSign = s.CallSign, Description = s.StationDescription };
        return estimateDetails;

    }

Controller

   public JsonResult EstimateDetails(int id)
    {
        var details = estimatesRepository.GetEstimateDetails(id);
        return this.Json(new { details = details }, JsonRequestBehavior.AllowGet);
    }

Resulting JSON

{"details":[["Albany-Schenectady-Troy","WRGB","WRGB (CBS) Schenectady"],["Albany-Schenectady-Troy","WTEN","WTEN (ABC) Albany "],["Albany-Schenectady-Troy","WXXA","WXXA (Fox) Albany "],["Atlanta","WGCL","WGCL (CBS) Atlanta "],["Atlanta","WXIA","WXIA (NBC) Atlanta "],["Austin","KXAN","KXAN (NBC) Austin "],["Austin","KVUE","KVUE (ABC) Austin "],["Baltimore","WMAR","WMAR (ABC) Baltimore "],["Baltimore","WBAL","WBAL (NBC) Baltimore"],["Baltimore","WJZ ","WJZ  (CBS) Baltimore "],["Baltimore","WBFF","WBFF (Fox) Baltimore "]]}

EDIT - here's my EstimateDetailsModel class

public class EstimateDetailsModel : IEnumerable<string>
{
    public string Dma { get; set; }

    public string CallSign { get; set; }

    public string Description { get; set; }

    public IEnumerator<string> GetEnumerator()
    {
        yield return Dma;
        yield return CallSign;
        yield return Description;
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}

Upvotes: 0

Views: 617

Answers (1)

Joe
Joe

Reputation: 82624

You need to remove the enumerator code from your model. It is overriding the default behavior.

Upvotes: 2

Related Questions