2012-06-01 107 views
0

我在我的控制器中使用以下操作,我試圖顯示jq.grid中的房間數量;jsonresult錯誤結果

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> 

而是獲得這樣的事: enter image description here

我得到這個:

enter image description here

我在做什麼錯誤?

+0

你應該怎麼樣? –

+0

問題是,我得到這個{「標題」:「總計」,「Rezervari」:4}而不是我的觀點,我有一個JavaScript,我想顯示在jq.grid – jonny

+0

,而不是什麼? –

回答

2

這是因爲在您的控制器中,您返回了一個JsonResult,它使json對象成爲視圖。
相反,您應該返回ViewResultPartialViewResult並確保您將該對象作爲參數傳遞給它。