2011-07-15 194 views
0

我有以下的Web服務:爲什麼webservice返回null?

<%@ WebService Language="VB" Class="WebService" %> 

Imports System.Web 
Imports System.Web.Script.Services 
Imports System.Web.Services 

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
<System.Web.Script.Services.ScriptService()> _ 
<WebService(Namespace:="http://tempuri.org/")> _ 
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ 
Public Class WebService 
    Inherits System.Web.Services.WebService 

    Public Class Person 
     Public FirstName As String 
     Public LastName As String 

     Public Sub New(ByVal m_FirstName As String, ByVal m_LastName As String) 
     End Sub 
    End Class 

    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _ 
    <WebMethod()> _ 
    Public Function GetPersons() As List(Of Person) 
     Dim lst As List(Of Person) = New List(Of Person) 

     lst.Add(New Person("firstname_1", "surname_1")) 
     lst.Add(New Person("firstname_2", "surname_2")) 

     Return lst 
    End Function 
End Class 

但它返回:

{"d":[{"FirstName":null,"LastName":null},{"FirstName":null,"LastName":null}]} 

它應該返回:

{"d":[{"FirstName":"firstname_1","LastName":"surname_1"},{"FirstName":"firstname_2","LastName":"surname_2"}]} 

我在做什麼錯?

回答

2

你永遠不會在構造函數中分配FirstName和LastName,所以它們的值將保持爲null。 以下內容添加到您的構造函數:

FirstName = m_FirstName 
LastName = m_LastName 
+0

就是這樣!謝謝。 – oshirowanen

0

我VB是有點生疏,但我沒有看到你的Person類的名字和姓氏的屬性。我只看到變量。 (隨着前面提到的缺失任務)