【问题标题】:Is there diference in this two JSON objects, look like there is no, but这两个json对象有区别吗,貌似没有,但是
【发布时间】:2015-07-26 13:43:13
【问题描述】:

当我从函数(硬编码)发送数据(值和日期)时,一切正常,json 文件被填充,我看到了图表,但是当我从数据库发送数据时,没有图表但我看到了json 文件也被填充。

代码如下:

public class YearlyStat
{
    public string year { get; set; }
    public double value { get; set; }
}

public ActionResult Statistics(int? id)
{
    //var result = db.pricepoints.Where(r => r.commodityID.Equals(id));
    var items = from item in db.pricepoints
                where (item.commodityID == id)
                select item;

    var stats = new List<YearlyStat>();

    foreach (var item in items)
    {
        stats.Add(new YearlyStat
        {
            year = item.date_of_price.ToShortDateString(),
            value = item.value
        });

    }
    //but this works
    //string s = "2.2.2002";
    //double v = 20.20;
    //stats.Add(new YearlyStat { year = s, value = v });
    //or
    //stats.Add(new YearlyStat { year = "2.2.2002", value = 20.20 });

    return Json(stats, JsonRequestBehavior.AllowGet);
}

在这两种情况下,类型都是字符串和双精度。

【问题讨论】:

  • 您是否查看了响应以了解有什么不同?
  • 它现在可以工作了,这段代码是正确的,问题出在我的 javascript 中,我没有将 id 参数发送到这个函数。 Tnx :)

标签: javascript asp.net json asp.net-mvc-4


【解决方案1】:

我明白了:D

问题出在我的 JavaScript 中,我没有将“id”参数发送到统计函数。

<script type="text/javascript">
$.get('@Url.Action("Statistics")', function(result){
    new Morris.Line({
        // ID of the element in which to draw the chart.
        element: 'myfirstchart',
        // Chart data records -- each entry in this array corresponds to a point on
        // the chart.
        data: 
          result
        ,
        // The name of the data record attribute that contains x-values.
        xkey: 'year',
        // A list of names of data record attributes that contain y-values.
        ykeys: ['value'],
        // Labels for the ykeys -- will be displayed when you hover over the
        // chart.
        labels: ['Value','Date']
    });
});

当我把它工作:

@Url.Action("Statistics", new { id = ViewBag.commodityID })

现在我很快乐:D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    相关资源
    最近更新 更多