我有一个部分视图,当控制器首次加载时,会像下面显示的那样在jquery数据表中加载数据。但是一旦我运行事件并调用部分操作方法,数据仍然返回,但格式已经消失了:
返回部分视图的代码:
public PartialViewResult ListAppointments(string _userId)
{
var userId = Convert.ToInt32(_userId);
var o = (from s in db.tblAppointments.ToList()
where s.UserId == userId
select new AppointmentViewModel { AppointmentInstructorName = s.InstructorName, AppointmentLessonAddress = s.Address, LessonDateTime = s.LessonDate, UserId = s.UserId, Id = s.ID });
return PartialView(o);
}
jQuery调用:
function success(result) {
var Id = $('#UserId').val();
var data = JSON.stringify({"_userId": Id});
$.ajax({
type: "GET",
url: "@Url.Action("ListAppointments", "Appointment")",
data: data,
success: function (result2) { $("#partialViewAppointments").html(result2); }
});
}
Razor中的局部视图是:
<div class="panel-heading tabbable" tabindex="0"><h1>List of all appointments (including historical) for user</h1></div>
<div class="panel-body" id="partialViewAppointments">
@Html.Partial("ListAppointments", Model.Appointments)
</div>
</div>
局部视图:
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Instructor</th>
<th>Lesson Date and Time</th>
<th>Address (if different)</th>
<th></th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>Instructor</th>
<th>Lesson Date and Time</th>
<th>Address (if different)</th>
<th></th>
</tr>
</tfoot>
<tbody>
@if (Model != null)
{
foreach (var info in Model)
{
<tr>
<td>@info.Id</td>
<td>@info.AppointmentInstructorName</td>
<td>@info.LessonDateTime</td>
<td>@info.AppointmentLessonAddress</td>
<td>@Html.ActionLink("Edit", "Edit", "Appointment", new { id = info.Id }, null)</td>
</tr>
}
}
</tbody>
</table>