2013-05-30 19 views
0

人視圖模型:FormView控件上的對象與IEnumerable的財產

public class Person { 
    public string Name { get; set; } 
    public List<Person> Relatives { get; set; } 
    public bool IsSelected { get; set; } 
} 

我有我的網頁上的asp:FormView控制包含這勢必集合屬性我Person類的asp:GridView

<asp:FormView ID="ui_frmMain" runat="server" DefaultMode="Edit" OnCallingDataMethods="ui_frmMain_CallingDataMethods" SelectMethod="GetItems" UpdateMethod="UpdateItems" ItemType="Person"> 
    <EditItemTemplate> 
     <%# Item.Name %> 
     <asp:GridView ID="..." runat="server" DataSource='<%# Bind("Relatives") %>' ItemType="Person"> 
      <ItemTemplate> 
         <asp:TemplateField> 
          <ItemTemplate> 
           <asp:CheckBox ID="Selected" runat="server" Checked='<%# Bind("IsSelected") %>' /> 
          </ItemTemplate> 
         </asp:TemplateField> 
      </ItemTemplate> 
     </asp:GridView> 
    <EditItemTemplate> 
</asp:FormView> 

我使用.NET 4.5數據綁定到我的模型發送回UpdateMethod UpdateItems(Person person)但是Relatives屬性始終爲空。

有沒有一種辦法:從一個GridView

  • 綁定GridView的複選框,在模型上IsSelected財產

    • 雙向綁定
  • +0

    您能否提供更多信息? –

    +0

    我已經添加了一些更多詳細信息 - 謝謝 – Darbio

    +0

    嘗試使用Checked =「<%#Item.IsSelected)%>」在複選框中綁定到您的Person實體。或以其他方式使用Checked =「<%#BindItem.IsSelected)%>」。 – Neville

    回答

    0

    之前保存的更新變化人對象「手動」循環並逐個添加嵌套的集合對象。

    例如:

    UpdateItems(Person person) 
    { 
        // :(probably null for Relatives :(
        GridView myGrid = (GridView)FindControl("MyGrid"); 
        for (int i = 0; i < myGrid.Rows.Count; i++) 
        { 
         Textbox txtName = (Textbox)myGrid.Rows[i].FindControl(""); 
         CheckBox chkSelected = (Checkbox)myGrid.Rows[i].FindControl(""); 
         Person p = new Person(); 
         p.Name = txtName.Text; 
         p.IsSelected = chkSelected.cheked; 
         person.Relatives.Add(p); 
        } 
        myEntity.SaveChanges(); 
    

    這種方法是可怕的,但我還沒有看到任何將認識到嵌套集合中的變化呢。