ASP.NET 双向绑定具有可变数量项目的嵌套列表视图
ASP.NET two-way bind a nested listview with a variable amount of items
我正在尝试了解如何对 ListView 进行双向数据绑定。从 MVC 到 Web 窗体应用程序,我正在为如何做而苦苦挣扎。
我创建了一个视图模型结构,它表示 SharePoint 应用程序中的嵌套层次结构。我希望针对我的视图模型进行双向绑定,然后我可以将其转换为我的 SharePoint 数据源:
Line of Business
Solution Line
Product Code (user can "check" if the product code should be applied)
查看模型层次结构:
public class AddCustomerViewModel
{
public IList<LineOfBusinessViewModel> LinesOfBusiness { get; set; }
public AddCustomerViewModel()
{
LinesOfBusiness = new List<LineOfBusinessViewModel>();
}
}
public class LineOfBusinessViewModel
{
public string Title { get; set; }
public IList<SolutionLineViewModel> SolutionLines { get; set; }
public LineOfBusinessViewModel()
{
SolutionLines = new List<SolutionLineViewModel>();
}
}
public class SolutionLineViewModel
{
public string Title { get; set; }
public IList<ProductCodeViewModel> ProductCodes { get; set; }
public SolutionLineViewModel()
{
ProductCodes = new List<ProductCodeViewModel>();
}
}
public class ProductCodeViewModel
{
public string Title { get; set; }
public bool IsChecked { get; set; }
}
ASPX 页面:
<asp:ListView ID="LineOfBusinessListView" ClientIDMode="Static" runat="server" ItemType="ViewModel.AddCustomer.LineOfBusinessViewModel" ItemPlaceholderID="lobPlaceHolderId">
<LayoutTemplate>
<ul>
<asp:PlaceHolder runat="server" ID="lobPlaceHolderId"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<%# Item.Title %>
<asp:ListView ID="SolutionLinesView" ClientIDMode="Static" runat="server" DataSource="<%# Item.SolutionLines %>" ItemType="ViewModel.AddCustomer.SolutionLineViewModel" ItemPlaceholderID="solutionLinePlaceHolderId">
<LayoutTemplate>
<ul>
<asp:PlaceHolder runat="server" ID="solutionLinePlaceHolderId"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<%# Item.Title %>
<asp:ListView ID="ProductCodeView" ClientIDMode="Static" runat="server" DataSource="<%# Item.ProductCodes %>" ItemType="ViewModel.AddCustomer.ProductCodeViewModel" ItemPlaceholderID="productCodePlaceHolderId" OnDataBinding="ProductCodeView_DataBinding">
<LayoutTemplate>
<ul>
<asp:PlaceHolder runat="server" ID="productCodePlaceHolderId"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<%# Item.Title %>
<asp:CheckBox ID="productCode" Checked="<%# BindItem.IsChecked %>" runat="server" />
</li>
</ItemTemplate>
</asp:ListView>
</li>
</ItemTemplate>
</asp:ListView>
</li>
</ItemTemplate>
隐藏代码:
protected AddCustomerViewModel AddCustomerViewModel { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
AddCustomerViewModel = new AddCustomerViewModel();
// Construct view model heiarchy...
LineOfBusinessListView.DataSource = AddCustomerViewModel.LinesOfBusiness;
LineOfBusinessListView.DataBind();
}
}
如何正确地将视图模型双向绑定到嵌套的 ListView(单向绑定有效),以及如何在 post-back/form-submission 上检索这些绑定值?
编辑:
我注意到一件尴尬的事情是,为了将 BindItem
用于复选框元素,我需要定义一个 ID。但是,这些项目是动态的,因此定义一个 "static" ID 是行不通的,我不确定如何动态设置一个 ID。
ASP.NET Web 窗体 4.5,c#
我找不到一个直接的答案。可以解析 Request.form.Allkeys
以获得已发布的 name/value 对,但这变得非常复杂,非常快。
对于我们的解决方案,由于这是一个 "green" 项目,我们最终将服务器端网络调用切换到 .NET Web API 2,这确实简化了绑定。
我正在尝试了解如何对 ListView 进行双向数据绑定。从 MVC 到 Web 窗体应用程序,我正在为如何做而苦苦挣扎。
我创建了一个视图模型结构,它表示 SharePoint 应用程序中的嵌套层次结构。我希望针对我的视图模型进行双向绑定,然后我可以将其转换为我的 SharePoint 数据源:
Line of Business Solution Line Product Code (user can "check" if the product code should be applied)
查看模型层次结构:
public class AddCustomerViewModel
{
public IList<LineOfBusinessViewModel> LinesOfBusiness { get; set; }
public AddCustomerViewModel()
{
LinesOfBusiness = new List<LineOfBusinessViewModel>();
}
}
public class LineOfBusinessViewModel
{
public string Title { get; set; }
public IList<SolutionLineViewModel> SolutionLines { get; set; }
public LineOfBusinessViewModel()
{
SolutionLines = new List<SolutionLineViewModel>();
}
}
public class SolutionLineViewModel
{
public string Title { get; set; }
public IList<ProductCodeViewModel> ProductCodes { get; set; }
public SolutionLineViewModel()
{
ProductCodes = new List<ProductCodeViewModel>();
}
}
public class ProductCodeViewModel
{
public string Title { get; set; }
public bool IsChecked { get; set; }
}
ASPX 页面:
<asp:ListView ID="LineOfBusinessListView" ClientIDMode="Static" runat="server" ItemType="ViewModel.AddCustomer.LineOfBusinessViewModel" ItemPlaceholderID="lobPlaceHolderId">
<LayoutTemplate>
<ul>
<asp:PlaceHolder runat="server" ID="lobPlaceHolderId"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<%# Item.Title %>
<asp:ListView ID="SolutionLinesView" ClientIDMode="Static" runat="server" DataSource="<%# Item.SolutionLines %>" ItemType="ViewModel.AddCustomer.SolutionLineViewModel" ItemPlaceholderID="solutionLinePlaceHolderId">
<LayoutTemplate>
<ul>
<asp:PlaceHolder runat="server" ID="solutionLinePlaceHolderId"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<%# Item.Title %>
<asp:ListView ID="ProductCodeView" ClientIDMode="Static" runat="server" DataSource="<%# Item.ProductCodes %>" ItemType="ViewModel.AddCustomer.ProductCodeViewModel" ItemPlaceholderID="productCodePlaceHolderId" OnDataBinding="ProductCodeView_DataBinding">
<LayoutTemplate>
<ul>
<asp:PlaceHolder runat="server" ID="productCodePlaceHolderId"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<%# Item.Title %>
<asp:CheckBox ID="productCode" Checked="<%# BindItem.IsChecked %>" runat="server" />
</li>
</ItemTemplate>
</asp:ListView>
</li>
</ItemTemplate>
</asp:ListView>
</li>
</ItemTemplate>
隐藏代码:
protected AddCustomerViewModel AddCustomerViewModel { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
AddCustomerViewModel = new AddCustomerViewModel();
// Construct view model heiarchy...
LineOfBusinessListView.DataSource = AddCustomerViewModel.LinesOfBusiness;
LineOfBusinessListView.DataBind();
}
}
如何正确地将视图模型双向绑定到嵌套的 ListView(单向绑定有效),以及如何在 post-back/form-submission 上检索这些绑定值?
编辑:
我注意到一件尴尬的事情是,为了将 BindItem
用于复选框元素,我需要定义一个 ID。但是,这些项目是动态的,因此定义一个 "static" ID 是行不通的,我不确定如何动态设置一个 ID。
ASP.NET Web 窗体 4.5,c#
我找不到一个直接的答案。可以解析 Request.form.Allkeys
以获得已发布的 name/value 对,但这变得非常复杂,非常快。
对于我们的解决方案,由于这是一个 "green" 项目,我们最终将服务器端网络调用切换到 .NET Web API 2,这确实简化了绑定。