Angular js JSON数据与DateTime的MVC模型绑定?

时间:2022-01-07 13:09:51

I am using Angular JS to get and post data to my MVC 4 controller as JSON. All of my properties convert properly except the DateTime/DateTime? types. I would like to have a 'set it and forget it' approach to handling dates so that new classes and or properties that are added will not have to remember to do some special conversion to handle a date properly. I have seen the following approaches and possible disadvantages. What approach are people out there using for the same platform? Is there something in MVC4 that is handling this properly that I may not have configured? Any suggestions will be greatly appreciated.

我正在使用Angular JS来获取和发布数据到我的MVC 4控制器作为JSON。除DateTime / DateTime外,我的所有属性都正确转换?类型。我希望有一个“设置并忘记它”处理日期的方法,以便添加的新类和/或属性不必记住进行一些特殊的转换以正确处理日期。我已经看到了以下方法和可能的缺点。人们在同一平台上使用什么方法? MVC4中有没有正确处理这些我可能没有配置的东西?任何建议将不胜感激。

  1. Custom model binder. As details in Phil Haack blog. Possible performance issues.
  2. 定制模型活页夹。正如Phil Haack博客中的细节。可能的性能问题。
  3. Perform some modification on the JS side. As detailed in Robert Koritnik's blog. This doesn't seem to work for Angular js, perhaps there is a setting in the $http.post that will allow this to work but my object has all null values with this approach.
  4. 在JS方面进行一些修改。详见Robert Koritnik的博客。这似乎不适用于Angular js,也许$ http.post中有一个设置允许它工作,但我的对象使用这种方法具有所有空值。
  5. Have some additional property such as FormattedDateTime that can be converted on the POST action. This isn't a 'set it and forget it approach' though it does allow for properly displaying dates in the client side which are still in the '/Date(695573315098)/' format
  6. 有一些额外的属性,如FormattedDateTime,可以在POST操作上转换。虽然它确实允许在客户端正确显示仍处于'/ Date(695573315098)/'格式的日期,但这不是'设置并忘记它的方法'

Please see the following code example. C# class:

请参阅以下代码示例。 C#类:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDate { get; set; }
    public DateTime? ApprovedForSomething { get; set; }
}

Angular JS Controller:

Angular JS控制器:

    function PersonController($scope, $http) {
        $scope.getPerson = function () {
            $http.get('../../Home/GetPerson/1').success(function (data) {
                $scope.Person = data;
            });
        }
        $scope.updateApprovedForSomething = function () {
            $http.post('../../Home/UpdatePerson', { person: $scope.Person }).success(function (data) {
                console.log(data);
            });
        }
    }

Fiddlers POST:

小提琴手:

HTTP/1.1 200 OK Cache-Control: private Content-Type: application/json; charset=utf-8 Server: Microsoft-IIS/8.0 X-AspNetMvc-Version: 4.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?YzpcdXNlcnNcbmlja1xkb2N1bWVudHNcdmlzdWFsIHN0dWRpbyAyMDEyXFByb2plY3RzXFZhbGlkYXRpb25UZXN0XEhvbWVcR2V0UGVyc29uXDE=?= X-Powered-By: ASP.NET Date: Wed, 16 Jan 2013 14:48:34 GMT Content-Length: 124

HTTP / 1.1 200 OK Cache-Control:private Content-Type:application / json;字符集= UTF-8服务器:Microsoft-IIS / 8.0 X-AspNetMvc-版本:4.0 X-ASPNET-版本:4.0.30319 X-SourceFiles:????= UTF-的8B YzpcdXNlcnNcbmlja1xkb2N1bWVudHNcdmlzdWFsIHN0dWRpbyAyMDEyXFByb2plY3RzXFZhbGlkYXRpb25UZXN0XEhvbWVcR2V0UGVyc29uXDE = = X供电,通过: ASP.NET日期:2013年1月16日星期三14:48:34 GMT内容长度:124

{"FirstName":"Bob","LastName":"Smith","BirthDate":"/Date(695573315098)/","ApprovedForSomething":"/Date(1358261315098)/"}

{ “姓”: “鲍勃”, “名字”: “史密斯”, “出生日期”: “/日期(695573315098)/”, “ApprovedForSomething”: “/日期(1358261315098)/”}

As you can see with the Fiddler data the date is coming over as a JSON date but when hitting the POST method the DateTime property is not correct and the DateTime? property is null.

正如您在Fiddler数据中看到的那样,日期将作为JSON日期过来,但是当遇到POST方法时,DateTime属性不正确并且DateTime? property为null。

Angular js JSON数据与DateTime的MVC模型绑定?

1 个解决方案

#1


3  

Do you have the option of switching your AJAX postback to be handled by a WebApi's ApiController instead of an MVC controller?

您是否可以选择将您的AJAX回发切换为由WebApi的ApiController而不是MVC控制器处理?

I ran into this problem recently and took advantage of the ApiController's support for ISO 8601 date format.

我最近遇到了这个问题并利用了ApiController对ISO 8601日期格式的支持。

Here's some info: http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx

以下是一些信息:http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx

#1


3  

Do you have the option of switching your AJAX postback to be handled by a WebApi's ApiController instead of an MVC controller?

您是否可以选择将您的AJAX回发切换为由WebApi的ApiController而不是MVC控制器处理?

I ran into this problem recently and took advantage of the ApiController's support for ISO 8601 date format.

我最近遇到了这个问题并利用了ApiController对ISO 8601日期格式的支持。

Here's some info: http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx

以下是一些信息:http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx