0

我正在创建部分视图。我需要获取自 2012.04.01 以来经过的秒数

public PartialViewResult SitePowered()
{
    var dateTime = new DateTime(2012, 4, 1);
    var totalSeconds = (DateTime.Now - dateTime).TotalSeconds;
    ViewBag.TotalSeconds = totalSeconds;
    return PartialView("SitePowered");
}

看法:

<script type="text/javascript">
    $(function () {
        $.getJSON("/Home/SitePowered", null, setTime);
    });
</script>
@ViewBag.TotalSeconds секунд 

但它不起作用

4

1 回答 1

1

你必须做这样的事情:

行动:

public ActionResult SitePowered()
{
    var dateTime = new DateTime(2012, 4, 1);
    var totalSeconds = (DateTime.Now - dateTime).TotalSeconds;
    return Json(new { seconds = totalSeconds });
}

客户端:

<script type="text/javascript">
    $(function () {
        $.getJSON("/Home/SitePowered", function(result) {
              // Do something here with result.seconds
              alert(result.seconds);
        });
    });
</script>

希望能帮助到你。

于 2012-04-25T11:44:22.240 回答