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

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

问题描述

我最近开始使用 KnockoutJs 并很快意识到使用默认的 Json(myModelWithADate) 导致了 /Date(-62135578800000)/ 的默认 json 编码经过一些研究,我找到了四种可能的方法来处理 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 指定在 james.newtonking.com

示例

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

4) 使用 JSON.parse 来处理您的日期,如此 stackoverflow 答案所示.

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解决方案是最好的,因为它对客户的影响较小.所有其他解决方案都需要额外的客户端解析或额外的客户端代码.

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 的所有 ASP .NET 代码,我已转而使用 JSON.NET,因为它是一个更加可定制的库.

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 中实现符合 Google 的图表 API(与 Knockout 结合用于分页等)和默认的 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,您还可以自定义它以实际输出完整的 Knockout 视图模型,因此您甚至不需要使用映射插件.

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 的示例库,它可以让您在 Razor 中执行以下操作:

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

因此,您可以获得一个 Knockout 视图模型,而无需跳过任何客户端箍.

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

您可以轻松地扩展类似的东西来处理您喜欢的日期值.

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

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

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