角JS与MVC4模型绑定日期/日期时间失败? [英] Angular JS with MVC4 model binding failing with date/datetime?

查看:166
本文介绍了角JS与MVC4模型绑定日期/日期时间失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在采用了棱角分明的JS回发到一个复杂的对象,日期时间和日期时间服务器?值不正确地结合。我曾尝试JSON.stringify无济于事。我已经发布了相关的问题,虽然可能它太一般。我真正需要知道的是如何正确地传递这些日期在什么我目前做的是用替代方法的js的日期转换,但我宁愿不这样做,只是得到的形式,我需要他们的时候在角日期然后传回正确的值。

When using Angular JS to post back to the server with a complex object the datetime and datetime? values do not bind correctly. I have tried JSON.stringify to no avail. I have posted a related question though possibly it was too general. What I really need to know is how to correctly pass those dates in. What I am currently doing is using workaround in js to convert the dates but I would rather not do that and simply get the dates in the form I need them when in Angular and then pass back the correct values.

你如何绑定到这些日期时间/日期?值是否正确?请看下面的code为例,提琴手后的结果。

How do you bind to those datetime/datetime? values correctly? Please see the following code example and Fiddler post results.

C#类:

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

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

提琴手帖子:

HTTP / 1.1 200 OK的Cache-Control:私人内容类型:应用程序/ JSON的;
  字符集= utf-8的服务器:Microsoft-IIS / 8.0的X AspNetMvc-版本:4.0
  的X ASPNET-版本:4.0.30319的X来源档案:
  =?UTF-8?B?YzpcdXNlcnNcbmlja1xkb2N1bWVudHNcdmlzdWFsIHN0dWRpbyAyMDEyXFByb2plY3RzXFZhbGlkYXRpb25UZXN0XEhvbWVcR2V0UGVyc29uXDE=?=
  的X技术,通过:ASP.NET日期:星期三,2013年1月16日格林尼治标准​​时间十四点48分34秒
  内容长度:124

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

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

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

这是在服务器端的结果。日期时间绑定到一个新的datetime值这是不正确和日期时间?为空。

This is the result on the server side. The datetime binds to a new datetime value which is not correct and the datetime? is null.

推荐答案

如果有人有更好的解决方案,然后请随时更新的答案。

If someone has better solution then please feel free to update the answer.

有可能是更好的解决方案在那里,但我所做的是很简单的解决方法。
刚创建DateTime对象的封装属性为字符串,并使用绑定的目的。

There might be better solution out there but what I did is very simple workaround. Just create an encapsulation property for DateTime object to string and use that for binding purpose.

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDate { get; set; }
    public DateTime? ApprovedForSomething { get; set; }
    public DateTime BirthDateAsString 
    {
        get { return BirthDate.ToShortDateString();}
        set { DateTime.Parse(value, BirthDate);}
   }
}

在HTTP上的所有对象都被视为字符串,但ASP.NET足够聪明,提供模型绑定功能。然而,它是无法绑定JavaScript的Date对象.NET 的DateTime 对象。

这篇关于角JS与MVC4模型绑定日期/日期时间失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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