【问题标题】:FormView on Object with IEnumerable property具有 IEnumerable 属性的对象上的 FormView
【发布时间】:2013-05-30 05:09:34
【问题描述】:

人视图模型:

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的CheckBox绑定到模型的IsSelected属性

【问题讨论】:

  • 您能提供更多信息吗?
  • 我添加了更多细节 - 谢谢
  • 尝试在 Checked="" 的复选框中绑定到您的 Person 实体。或者以其他方式使用 Checked=""。

标签: c# asp.net .net data-binding .net-4.5


【解决方案1】:

在“手动”保存更新的人员对象上的更改之前,请循环并一一添加嵌套的集合对象。

例如:

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();

这种方法很糟糕,但我还没有看到任何可以识别嵌套集合中的变化的东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多