2017-06-06 43 views
0

我想從ajax調用控制器,併發送模型數據,其中包含一些數據裏面的數據。我得到了他們正確的計數,但在列表中的屬性是空的。如何發送一個模型,其中包含通用列表從ajax調用mvc C#

控制器:

public JsonResult InsertorUpdate(IncidentEdit incident) 
     { 
      try 
      { 

       SrvincidentDtls.TicketNo = incident.TicketNo; 
       SrvincidentDtls.Priority = incident.Priority; 
       SrvincidentDtls.Status = incident.Status; 
       SrvincidentDtls.Title = incident.Title; 

       SrvincidentDtls.IsActive = incident.IsActive; 
       List<Comments> lstComm = new List<Comments>(); 
       if(incident.CommentList!=null) 
       { 
        foreach (var comm in incident.CommentList) 
        { 
         Comments Comm = new Comments(); 
         Comm.Comment = comm.Comments; 
         Comm.AddedBy = comm.AddedBy; 
         Comm.AddedBy_ID = comm.AddedBy_ID; 
         Comm.CreatedDate = comm.CreatedDate; 
         lstComm.Add(Comm); 

        } 
       } 

       SrvincidentDtls.Comments = lstComm.ToArray(); 
       SrvincidentDtls.Description = incident.Description; 



       var result=proxyService.InsertorUpdateIncidentDetails(SrvincidentDtls); 
       return Json(new { success = true, jvalue = result }, JsonRequestBehavior.AllowGet); 

      } 
      catch(Exception ex) 
      { 
       throw ex; 
      } 

     } 

型號:

public class IncidentEdit 
    { 


     public string Title { get; set; } 

     public string Description { get; set; } 

     public bool IsActive { get; set; } 

     public string Status { get; set; } 

     public string Priority { get; set; } 
     public List<IncidentComments> CommentList { get; set; } 
    } 

泛型類:

public class IncidentComments 
    { 
     public string Comments { get; set; } 
     public string AddedBy { get; set; } 
     public string AddedBy_ID { get; set; } 
     public DateTime CreatedDate { get; set; } 
    } 

Ajax調用:

function InsertOrUpdate(){ 
      incidentDetails = { 

         Title: $("#txtTitle").val(), 
         Description: $("#txtDescription").val(), 
         Priority: $('#selPriority option:selected').val(), 
         Status: $('#selStatus option:selected').val(), 
         IsActive: 1, 
         CreatedDate :$('#spncrtdDt').text(), 
         CommentList:PopulateCommentList() 

        }; 

        $.ajax({ 


          type: "Get", //HTTP POST Method 
          url: insertORupdatUrl, // Controller/View 
          data: {incident:incidentDetails}, 
          contentType: "application/json; charset=utf-8", 
          success: function (data) { 
    } 
    }); 
    } 

js函數:

function PopulateCommentList() { 

      var CommentList = []; 
      var dtTable = $('#dvCommentTbl').DataTable(); 

      for (var i = 0; i < 10; i++) { 
    if (dtTable.row(i).data() != "" && dtTable.row(i).data() != null && dtTable.row(i).data() != undefined) { 
        CommentList.push({ 
         Comments: dtTable.row(i).data()[1], 
         AddedBy: dtTable.row(i).data()[2], 
         AddedBy_ID: dtTable.row(i).data()[0], 
         CreatedDate: dtTable.row(i).data()[3] 
        }); 
       } 


      } 

      return CommentList; 
     } 

發表評論列表中的計數來了很好,但像Added_By數據,評論,創建DTAE所有未來空。

Plz help。

+0

在你的控制器的Action方法中,你能夠調試結果的值,是否有適當的數據在裏面。這裏放一個斷點:return Json(new {success = true,jvalue = result},JsonRequestBehavior.AllowGet) ;並檢查結果的值。 –

+0

你能看到你的控制檯,並檢查你在邊評論列表屬性中發送的數據嗎?此外,如果你可以嘗試'JSON.parse(PopulateCommentList())' –

+0

JSON來罰款@ Abdul – raghav

回答

0

你可以把一個變量像

var commlist=PopulateCommentList(); 

然後在模型JSON填充它:

incidentDetails = { 

         Title: $("#txtTitle").val(), 
         Description: $("#txtDescription").val(), 
         Priority: $('#selPriority option:selected').val(), 
         Status: $('#selStatus option:selected').val(), 
         IsActive: 1, 
         CreatedDate :$('#spncrtdDt').text(), 
         CommentList:commlist 

        }; 

它會工作,因爲它爲我工作。

相關問題