我在我的控制器中使用以下Action,我试图在jq.grid中显示房间数;
public JsonResult Incercare()
{
DBContext.Current.Open();
var data = new
{
Title="Total",
Rezervari = Reservation.SelectByReservationDate(5).FirstOrDefault().NumarRezervari
};
var result = new JsonResult(){Data = data,JsonRequestBehavior =JsonRequestBehavior.AllowGet};
return result;
}
以下是我的观点:
@model LicentaTest.Models.Reservation
@{
ViewBag.Title = "Incercare";
}
<script type="text/javascript">
$(function () {
var requestUrl = '@Url.Action("Incercare","Grafice")';
$("#cartResults").jqGrid({
url: requestUrl,
datatype:'json',
mtype: 'GET',
emptyrecords: "No results",
colNames: ['Title','Rezervari'],
colModel: [
{ name: "Title", width: 350, editable: false, searchoptions: {}, index: "Title" },
{ name: "Rezervari", width: 100, editable: false, searchoptions: {}, index: "Rezervari"}
],
pager: $('#cartPager'),
rowList: [], // disable page size dropdown
pgbuttons: false, // disable page control like next, back button
viewrecords: false, // disable current view record text like 'View 1-10 of 100'
sortname: 'id',
sortorder: 'asc',
loadonce: false,
sortable: true,
scrollrows: true,
height: 300,
pgtext: null,
hoverrows: false,
footerrow: true,
userDataOnFooter: true,
emptyDataText: "Your Cart is Empty"
});
});
</script>
<div class="styler">
<fieldset class="ui-widget">
<legend class="ui-state-legend-default ui-corner-top ui-corner-bottom">Review Your Cart </legend>
<div id="update-message-disp" class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 0 .7em;width: 500px">
<p><span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"> </span><strong>Cart modified</strong><br />
</p>
</div>
<br />
<table id="cartResults">
</table>
<div id="cartPager">
</div>
</fieldset>
</div>
但不是得到这样的东西:
我明白了:
我做错了什么?
答案 0 :(得分:2)
这是因为在你的控制器中你返回一个JsonResult
,它将 json 对象呈现给视图。
相反,您应该返回ViewResult
或PartialViewResult
,并确保将对象作为参数传递给它。