【发布时间】:2014-09-23 11:40:33
【问题描述】:
我正在使用 MVC 5 应用程序,我试图通过 ajax 将数据传递到我的控制器,但我收到“语法错误:意外令牌
这是我的带有 javascript 的 html 页面。
@model webby.Models.Calendar
<div id="calendar">
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="padding:20.5% 15%;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Events on @ViewBag.Date</h4>
</div>
<div class="modal-body">
<table>
<tr>
<td>
@Html.DisplayFor( model=> model.events)
</td>
</tr>
<tr>
<td>
@Html.DisplayFor(model => model.type)
</td>
</tr>
<tr>
<td>
@Html.DisplayFor(model => model.content)
</td>
</tr>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function () {
$('#calendar').fullCalendar({
height: 170,
selectable: true,
editable: true,
defaultView: 'basicWeek',
dayClick: function (date, jsEvent, view) {
$.ajax(
{
url: '@Url.Action("Calendar","Home")',
type: "POST",
data: JSON.stringify({ date: date }),
dataType: "json",
contentType: "application/json; charset=utf-8",
cache: false,
success: function (response) {
alert(response);
},
error: function (request, status, error) {
alert(error);
alert(status);
alert(request.responseText);
}
});
$('#myModal').modal();
}
});
});
</script>
这是我的控制器:
[HttpPost]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public ActionResult Calendar(string date)
{
DateTime objDate = Convert.ToDateTime(date);
var content = db.Calendars.Where(x => x.startDate == objDate).Single();
return PartialView(content);
}
我相信这个错误导致数据在通过我的控制器的帖子推送后没有显示在我的视图中。
【问题讨论】:
标签: c# javascript asp.net ajax asp.net-mvc-5