我的JsonResult包含没有字段名称的值

时间:2012-01-18 04:28:12

标签: c# asp.net-mvc json

这里的问题从底部生成的JSON数组中非常明显。从我的代码中我无法确定为什么输出不包含字段名称(例如“dma”:“Albany-Schenectady-Troy”而不仅仅是“Albany-Schenectady-Troy”)。有人能发现问题可能是什么吗?

模型

    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;

    }

控制器

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

产生的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 "]]}

编辑 - 这是我的EstimateDetailsModel类

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();
    }
}

1 个答案:

答案 0 :(得分:2)

您需要从模型中删除枚举器代码。它覆盖了默认行为。