Even though i have overriden ToString method in the DataContract by returning ActivityType.Code property value in for class ActivityType. I'm still not geting the ActivityType.Code property value on a client for binding the grid.can one let me know how the ToString method be available to Client.
Here is the code base
Data Contract
[DataContract]
public class Activity
{
[DataMember]
public ActivityType ActivityTypeId { get; set; }
[DataMember]
public string Code { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public EntityStatus Status { get; set; }
}
[DataContract]
public enum EntityStatus
{
[EnumMember]
Active = 0,
[EnumMember]
Inactive = 1
}
[DataContract]
public class ActivityType
{
[DataMember]
public string Code { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Description { get; set; }
public override string ToString()
{
if (Code != null)
{
return Code.ToString(CultureInfo.InvariantCulture);
}
else
{
return base.ToString();
}
}
}
Service Implementation
public class TimeSheetManagementService : ITimeSheetManagementService
{
public List<BusinessEntities.Activity> GetActivities()
{
TimeSheetManagementDataController controller = new TimeSheetManagementDataController();
var activities = controller.GetActivities();
var activitiesresult = activities.Select(activity => new BusinessEntities.Activity()
{
ActivityTypeId =
new BusinessEntities.ActivityType()
{
Code = activity.ActivityType.Code,
Description =
activity.ActivityType.
Description,
Name = activity.ActivityType.Name
},
Code = activity.Code,
Description = activity.Description,
Status = (EntityStatus) Enum.Parse(typeof(EntityStatus),Convert.ToString((activity.Status==true)?1:0))
});
return activitiesresult.ToList();
}
}
Client Implementation
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField runat="server" DataField="Code"/>
<asp:BoundField runat="server" DataField="Description"/>
<asp:BoundField runat="server" DataField="Status"/>
<asp:BoundField runat="server" DataField="ActivityTypeId"/>
</Columns>
</asp:GridView>
Output
Code Description Status ActivityTypeId
ACT2 Requirement Feasibility Study Inactive TimeSheetManagementServiceRef.ActivityType
ACT1 Requirement analysis Inactive TimeSheetManagementServiceRef.ActivityType
Data contract is for transporting data - not for transporting logic. When you use add service reference, add web reference, xsd.exe, wsdl.exe or svcutil.exe only properties are described - all your methods declared on server are lost. There are two ways to solve this:
ToString
method obviously belongs to the client side because it is related to data presentation so create second partial part of the contract class on the client and declare ToString
method in that class.