I can't figure out how to use model binding in web forms to populate a child collection of a view model. Suppose I have the following:
public class MeasurementViewModel
{
public string Name {get;set;}
public int MeasurementTypeId {get;set;}
public double Value {get;set;}
}
public class ExperimentViewModel
{
public bool ExperimentAorB {get;set;}
public List<MeasurementViewModel> Measurements {get;set;}
}
So there are two types of experiments, A or B, each of which require a different set of measurements. I'd like to do this all on one page so that the user selects the experiment type, and is then presented with the list of measurements to enter via simple text boxes. This is simple in MVC, but I'm stuck with web forms where it's not clear how to do this.
<asp:DropDownList ID="ddExperiment" runat="server" CssClass="form-control" AutoPostBack="true"
DataValueField="Id" DataTextField="Description"
SelectMethod="ddPartCode_GetData" />
<asp:FormView ID="Entry" RenderOuterTable="false" runat="server" DefaultMode="Insert" Visible="<%# !string.IsNullOrEmpty(ddExperiment.SelectedValue) %>"
ItemType="ExperimentViewModel" SelectMethod="Entry_GetItem" InsertMethod="Entry_InsertItem">
<EditItemTemplate>
<asp:TextBox ID="txtName" Text="<%# BindItem.Name %>" runat="server" />
<!-- ??? -->
</EditItemTemplate>
</asp:FormView>
The insert method of the FormView is like this:
public void Entry_InsertItem(ExperimentViewModel item)
I've tried various markup in the ??? section, like a ListView with a SelectMethod with this signature:
public IEnumerable<MeasurementViewModel> Measurements_GetData(
[Control("ddExperiment")] int experimentType) ...
That works to display the proper form, but on postback ExperimentViewModel.Measurements is always null. I've also tried insert methods on the list view and a repeater to no avail.
I can obviously do this manually, but I figured there must be a standard way to do this using model binding. Obviously I have to define a control that actually binds to the ExperimentViewModel.Measurements, but I'm not sure how to do that in this setup.