2017-07-25 59 views
1

我從控制器獲取Id值到JavaScript代碼,但我想要全行值「reportssql.reportName」。並希望通過javascript在view中顯示reportName的值。從控制器獲取值到Javascript代碼

這裏,我的控制器

 [HttpGet] 
     public ActionResult ViewReports(int Id, SQLServer_Reports reportssql) 
     { 
      context = new Cost(); 
      //Todo get whole row values from ID, right now we have only Report ID 
      reportssql.reportName = context.SqlServerReportses.Where(x => x.ReportId == Id).Select(x=>x.reportName).FirstOrDefault(); 
      // List<string> report = context.SqlServerReportses.Where(x => x.ReportId == Id).Select(x=>x.reportName).ToList(); 

      return Json(reportssql.reportName, JsonRequestBehavior.AllowGet); 
     } 

這裏是我的javascript函數

function ViewReport(ID) { 
     var id = ID; 
     // alert(ID);// getting value from gridview button and passing to javascript alert 
     var state = {}; 
     var newUrl = "/Report/ViewReports/"; 
     //var newUrl="@Url.Action("ViewReports", "Report")"; 
     window.history.pushState(state, null, newUrl); 
     $.ajax({ 
      url: newUrl + ID, 
      type: "GET", 
      success: function() { 
       $('#ViewReports').show(); 
       $('#ShowGrid').hide(); 
       document.getElementById('lbltipAddedComment').innerHTML = 'Report ID = ' + id; 
       // display Report name here 
      }, 
      error: function() { 
       bootbox.alert("Error"); 
      } 

     }); 
     } 

像我的標籤顯示的ID值,在這裏,但也希望顯示所以reportName的vlaue以及在另一個標籤。如何獲取reportname我的意思是 返回Json(reportssql.reportName,JsonRequestBehavior.AllowGet); 在JavaScript函數中。

回答

0

在成功功能中,您需要使用從服務器發送的響應。改變你的成功功能是這樣的:

success: function (reportName) { 
    $('#ViewReports').show(); 
    $('#ShowGrid').hide(); 
    document.getElementById('lbltipAddedComment').innerHTML = 'Report ID = ' + id; 

    // set report name 
    $('#lblYourLabelIdHere').text(reportName); 
}