即使我通过在类 ActivityType 中返回 ActivityType.Code 属性值来覆盖 DataContract 中的 ToString 方法。我仍然没有在客户端上获取用于绑定网格的 ActivityType.Code 属性值。能否告诉我如何将 ToString 方法提供给客户端。
这是代码库
数据合约
[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();
}
}
}
服务实施
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();
}
}
客户端实施
<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>
输出
Code Description Status ActivityTypeId
ACT2 Requirement Feasibility Study Inactive TimeSheetManagementServiceRef.ActivityType
ACT1 Requirement analysis Inactive TimeSheetManagementServiceRef.ActivityType