我正在尝试将使用 jQuery DateTimePicker 选择的日期从输入字段发布到 ViewModel 中的 DateTime 属性。我不断得到 1/01/0001 0:00:00 的解析。让我们看看我的视图模型:
public class GameViewModel
{
[Display(Name = "Home team")]
public virtual String Home { get; set; }
[Display(Name = "Away team")]
public virtual String Away { get; set; }
[Display(Name = "Kickoff time")]
[DataType(DataType.DateTime)]
public virtual DateTime GameTime { get; set; }
[Display(Name = "Standard sharing on?")]
public virtual Boolean IsStandardSharing { get; set; }
//Hidden
public virtual Guid PeriodId { get; set; }
}
这是我的输入框剃刀:
@Html.EditorFor(m => m.GameTime)
最后是我的 JavaScript
$('#GameTime').datetimepicker({
constrainInput: true,
firstDay: 1,
monthNames: ['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December'],
monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec'],
dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
dayNamesMin: ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za'],
timeText: "Tijdstip",
hourText: "Uur",
minuteText: "Minuut",
currentText: "Nu",
closeText: "Klaar",
stepMinute: 5,
hour: 12
});
$('#btnAddGame').click(function () {
var testPeriodId = "245f83a2-52b5-4a21-815e-a0b40020d7ff";
var valDdlHome = $('#Home').val();
var valDdlAway = $('#Away').val();
var valTxtGameTime = Date($('#GameTime').val());
var valRdnIsStandardSharing = $('#IsStandardSharing').val();
var postUrl = "/period/saveperiodgame/";
$.post(
postUrl,
{ Home: valDdlHome, Away: valDdlAway, GameTime: valTxtGameTime, IsStandardSharing: valRdnIsStandardSharing, PeriodId: testPeriodId },
function (data) {
if (data.Status == 200) {
console.log("Game added. Id:" + data.Data);
if ($('#emptyText').length > 0) {
$('#emptyText').hide();
}
MijnProno.fn.BuildPeriodGameTableRow(valTxtGameTime, valDdlHome, valDdlAway, data.Data);
} else if (data.Status == 500) {
console.log("Failed to add game");
} else {
console.log("Fault with Javascript");
}
}, "json"
);
});
整个下午都在阅读并尝试回答 SO,但没有设法让它发挥作用(由于可能重复,我总是等很久才问这样的问题)。
在阅读了一些高度评价的帖子后,我想我必须像 /Date(1224043200000)/ 格式一样发布我的日期,以让我的 ViewModel 理解它。这是正确的吗?如果是这样,我怎样才能让我的 mm/dd/yyyy hh:ss 格式转换为 json?