I am a Beginner at JQuery(Json).I am not sure if it is possible or not to append DateTime to the Json table.I have a dropdown list.By selecting on a item its automatically load some data on table. For doing so I am trying a json function like this:
<script>
$(document).ready(function () {
$("#departmentId").change(function () {
var deptId = $("#departmentId").val();
//$("#courseId").empty();
var json = { departmentId: deptId };
$("#table").empty();
$("#table").append('<table class="table"><tr><th>Course Code</th><th>Name</th><th>Schedule Info</th></tr>');
$.ajax({
type: "POST",
url: '@Url.Action("ViewAllScheduleByDept", "ClassSchedule")',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(json),
success: function (data) {
$.each(data, function (key, value) {
$("#table").append('<tr><td>' + value.Code + '</td><td>' + value.Name + '</td><td>' + value.RoomName + ',' + ***value.StartTime.toDateString() +*** ' </td></tr>');
});
$('#table').append('</table>');
}
});
});
});
</script>
It Displays Code,Name and RoomName perfectly but if I add StartTime to append along with these three item, It shows nothing. Is there any possible way to convert DateTime right on the Json table-append?. If not then how can I do show date time along with Code,Name and room name on MVC Razor view.
Here is my Controller:
[HttpPost]
public JsonResult ViewAllScheduleByDept(int departmentId)
{
var schedules = GetAllSchedule();
var scheduleList = schedules.Where(a => a.DepartmentId == departmentId).ToList();
return Json(scheduleList, JsonRequestBehavior.AllowGet);
}
private List<ViewSchedule> GetAllSchedule()
{
List<ViewSchedule> allViewSchedules = scheduleManager.ViewAllSchedule();
return allViewSchedules;
}
}
And Model:
public class ViewSchedule
{
public int DepartmentId { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string RoomName { get; set; }
public DateTime StartTime { get; set; }
}
toDateStringcall on the StartTime property+ value.StartTime.toString() +$('#table').append('</table>')doesn't make sense.dataargument to your success function is that converted object, not JSON. The table is HTML that you're creating from the object.