在我的MVC5視圖中,我調用了一個JSON函數。 JSON函數返回一個AvaliableDates模型,它有一個Userld(字符串)和一個定義的DateTime對象列表。Ajax函數不顯示JSON日期列表(Mvc5)
我的視圖只能讀取Userld,但DateTime對象顯示爲「未定義」 任何人都可以看到爲什麼我沒有在我的視圖中獲取JSON DateTime LIST值嗎?
我的JSON功能:
public JsonResult GetFreeAppointmentDays()
{
List<DateTime> avaliableBookingDays = new List<DateTime>();
DateTime x1 = new DateTime(2017, 04, 11, 0, 0, 0);
DateTime x2 = new DateTime(2017, 04, 12, 0, 0, 0);
avaliableBookingDays.Add(x1.Date);
avaliableBookingDays.Add(x2.Date);
useravaliableDates AvaliableDates = new useravaliableDates()
{
UserIDs = "4,5,2,0,0,0,2",
Avdata = avaliableBookingDays
};
return Json(AvaliableDates, JsonRequestBehavior.AllowGet);
}
這是我的看法,用AJAX調用:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<input type="text" id="txtName" />
<input type="button" id="btnGet" value="Get Current Time" />
<div id="divshow"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("#btnGet").click(function() {
$.ajax({
type: "POST",
url: "/Home/GetFreeAppointmentDays",
dataType: "json",
success: function (data) {
$('#divshow').append(data);
var items = '';
$.each(data.Avdata, function (i, item) {
var row = "UserIDs are: " + data.UserIDs + " Date " + i + " -> " + item[i].Date + "<br/>"
$('#divshow').append(row);
});
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
});
});
</script>
</body>
</html>
怪:我的JSON函數返回此
{"UserIDs":"4,5,2,0,0,0,2","Avdata":["\/Date(1491861600000)\/","\/Date(1491948000000)\/","\/Date(1492034400000)\/","\/Date(1492207200000)\/","\/Date(1492466400000)\/"]}
它是爲你工作?我更新了我的答案。 –