处理与Asp.Net MVC和KnockoutJS日期 [英] Handling dates with Asp.Net MVC and KnockoutJS

查看:88
本文介绍了处理与Asp.Net MVC和KnockoutJS日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始KnockoutJs工作,并迅速使用默认的实现 JSON(myModelWithADate)导致 \\ /日期的默认JSON编码( - 62135578800000)\\ / 随着一点点研究,我设来处理我的日期的显示DOM元素四种可能的方式。

I recently started working with KnockoutJs and quickly realized using the default Json(myModelWithADate) resulted in the default json encoding of \/Date(-62135578800000)\/ With a bit of research I located four potential ways to handle the display of my dates in dom elements.

1)新建绑定处理来自JSON的日期格式,你的愿望转换

ko.bindingHandlers.date = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        var jsonDate = valueAccessor();
        var value = new Date(parseInt(jsonDate.substr(6)));
        var ret = value.getMonth() + 1 + "/" + value.getDate() + "/" + value.getFullYear();
        element.innerHTML = ret;
    },
    update: function(element, valueAccessor, allBindingsAccessor, viewModel) {

    }
};

用法

<td data-bind="date: DueDate">
</td>

2)返回从控制器串

return Json(new {MyDate = DateTime.Now.ToShortDateString()});

3)使用JSON.NET来指定<一看到过日期时间格式href=\"http://james.newtonking.com/archive/2009/02/20/good-date-times-with-json-net.aspx\">james.newtonking.com

示例

string isoJson = JsonConvert.SerializeObject(entry, new IsoDateTimeConverter());
// {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}

4)使用JSON.parse来处理您的日期,如本计算器答案可见。

JSON.parse(jsonText, function(key, value) {
    // Check for the /Date(x)/ pattern
    var match = /\/Date\((\d+)\)\//.exec(value);
    if (match) {
        var date = new Date(+match[1]); // Convert the ticks to a Date object
        return humanReadable(date); // Format the date how you want it
    }

    // Not a date, so return the original value
    return value;
});

他们似乎都工作,但我仍然挣扎与人觉得正确的。现在我的直觉与绑定并返回字符串组合去。正如我可以看到自己扩展的结合来处理输入与jQuery UI的日期选择控件。

They all appear to work, but I am still struggling with which one feels "right". Right now my gut is going with a mix with the binding and returning strings. As I could see myself extending the binding to handle input with jQuery UI datepicker controls.

是否有处理显示日期或其他类型的如货币时所接受的做法?难道还有其他的选择,我缺少解决这个问题?

推荐答案

我个人认为, JSON.NET 解决方案最好的,只是因为它少强加在客户端上。所有其他解决方案都需要额外的客户端解析或额外的客户端code。

Personally I think the JSON.NET solution is the best simply because it imposes less on the client. All the other solutions require additional client parsing or additional client code.

我已经切换到使用JSON.NET所有使用JSON,因为它更加定制库我的ASP .NET code的。

I have switched over to using JSON.NET for all of my ASP .NET code that uses JSON because its a much more customizable library.

例如,我不得不实施MVC JSON数据符合谷歌的图表API (在淘汰赛分页等组合使用)和默认的JavaScriptSerializer 根本无法做到这一点。

For example I have had to implement JSON data in MVC that conformed to Google's Chart API (used in combination with Knockout for paging, etc.) and the default JavascriptSerializer simply cannot do it.

在另外与JSON.NET你可以定制它实际上吐出完全敲除的视图模型,所以你甚至不需要聘请映射插件。

In addition with JSON.NET you can customize it to actually spit out full Knockout view models so you don't even need to employ the mapping plugin.

我编写了一个名为 FluentJson.NET 一个样本库,它可以让你在剃刀做这样的事情:

I wrote a sample library called FluentJson.NET which lets you do things in Razor like:

var viewModel = @JsonObject.Create()
    .AddProperty("name", "value")
    .AddObservable("knockoutProperty", 123)

和获取:

var viewModel = {"name":"value","knockoutProperty":ko.observable(123)}

所以,你可以无需任何客户端篮球淘汰赛视图模型跳通过。

So you can get a Knockout view model without any client side hoops to jump through.

您可以轻松地扩展类似的东西来处理日期值,但是你会preFER。

You could easily extend something like that to handle date values however you would prefer.

这篇关于处理与Asp.Net MVC和KnockoutJS日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆