2011-09-19 54 views
2

到GridView的我有使用ObjectDataSource控件實體如何將複雜的實體類型綁定使用的ObjectDataSource

class Person 
{ 
public int Age{get;set;} 
public string Name{get;set;} 
public Department Dept{get;set;} 
} 

class Department 
{ 
public int DeptId{get;set;} 
public string DeptName{get;set} 
} 

現在我結合收集到的GridView。 根據模板列的Person類的部門貌似

<EditItemTemplate> 
     <asp:DropDownList ID="cmbStatus" DataTextField="DeptName" SelectedValue='<%# Bind("Dept.DeptId") %>' 
      DataValueField="DeptId" runat="server" CssClass="ddl150px ddlbg" 
      DataSourceID="deptCollection" /> 
     <asp:ObjectDataSource ID="deptCollection" runat="server" 
        SelectMethod="GetDeptList" TypeName="LIMS.BusinessObject.Department" > 
    </asp:ObjectDataSource> 
</EditItemTemplate> 

現在我的網格被綁定使用現在

  <asp:ObjectDataSource ID="PersonCollection" runat="server" 
       SelectMethod="GetPersonList" 
       TypeName="LIMS.BusinessObject.Person" 
       DataObjectTypeName="LIMS.DomainModel.Person" 
       DeleteMethod="Delete" InsertMethod="Create" UpdateMethod="Update" 
       ondeleting="PersonCollection_Deleting" 
       onupdating="PersonCollection_Updating"> 
      </asp:ObjectDataSource> 

,當我嘗試更新這個人的實體,它會引發錯誤,因爲下拉顯示值字段和文本字段和Person實體需要一個實際綁定到下拉列表的Dept實體

回答

3

您不應該直接在表示層aspx中使用複雜實體。因爲如果域模型發生變化,例如添加一些字段,則會更新應用程序的每個aspx。你應該提供一個「視圖模型」是這樣的:

class PersonView 
{ 
    public int Age{get;set;} 
    public string Name{get;set;} 

    public int DeptId{get;set;} 
    public string DeptName {get { ... }} 
} 

它不應該是一個DTO:你不需要傳輸主機之間進行序列化的數據,但對於視圖的模式。

薄層可以將模型實體轉換爲查看模型。它可能是由ObjectDataSource很好支持的DataObject