Note: This is ASP.NET Web Forms Model Binding in .NET 4.5 and NOT MVC.
I am using the new Strongly Typed Model Binding features of ASP.NET Web Forms (4.5) to produce a list of items that can be edited. This is working fine for viewing the initial list, editing an item and deleting an item. I am however having a problem with the insertion of a new item.
Specifically, within my EditItemTemplate and InsertItemTemplate I have a DropDownList (well, actually it is a custom control derived from DropDownList but for the purposes of this question it is a DropDownList). The control is defined within the markup as follows...
<agp:ClientStatusDropDownList ID="ClientStatusID" runat="server"
SelectedValue="<%#: BindItem.ClientStatusID %>" />
Within the EditItemTemplate, this is fine however within the InsertItemTemplate this generates an error upon running the page: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
As such, I removed the section SelectedValue="<%#: BindItem.ClientStatusID %>"
from the InsertItemTemplate and tried again. This time no error message, however when the ListView.InsertMethod
is called, the ClientStatusID property on the model is not set to the value of the DropDownList (whereas the rest of the properties are set correctly).
The ListView.InsertMethod:
public void ListView_InsertMethod(int ID) {
Model model = this.DbContext.Models.Create();
if (this.TryUpdateModel(model)) {
this.DbContext.SaveChanges();
this.ListView.DataBind();
}
}
The Model class:
public class Model{
public Int32 ID { get; set; }
public String Description { get; set; }
public Boolean IsScheduleFollowUp { get; set; }
public Nullable<Int32> ClientStatusID { get; set; }
}
The EditItemTemplate:
<EditItemTemplate>
<tr>
<td>
<asp:TextBox ID="Description" runat="server" Text="<%#: BindItem.Description %>" />
</td>
<td>
<asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="<%# BindItem.IsScheduleFollowUp %>" />
</td>
<td>
<agp:ClientStatusDropDownList ID="ClientStatusID" runat="server" SelectedValue="<%#: BindItem.ClientStatusID %>" />
</td>
<td>
<asp:Button ID="Update" runat="server" ClientIDMode="Static" CommandName="Update" Text="Update" />
<asp:Button ID="Cancel" runat="server" ClientIDMode="Static" CommandName="Cancel" Text="Cancel" />
</td>
</tr>
</EditItemTemplate>
The InsertItemTemplate:
<InsertItemTemplate>
<tr>
<td>
<asp:TextBox ID="Description" runat="server" Text="<%#: BindItem.Description %>" />
</td>
<td>
<asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="<%# BindItem.IsScheduleFollowUp %>" />
</td>
<td>
<agp:ClientStatusDropDownList ID="ClientStatusID" runat="server" />
</td>
<td>
<asp:Button ID="Insert" runat="server" ClientIDMode="Static" CommandName="Insert" Text="Add" />
</td>
</tr>
</InsertItemTemplate>
I had originally thought that it was the ID of the control that was used to determine the property on the model that the value would be passed to (i.e. where the TextBox was called "Description", the value would be passed to the "Description" property of the model). Clearly this is not the case and it is instead controlled by the "<%# BindItem.Description %>", however as you can see from the rest of this question I am unable to use this syntax in the "InsertItemTemplate". I cannot believe that the DropDownList is not supported in this scenario, but I cannot find any examples of a DropDownList being used with the 4.5 model bindings using Google or Bing (in fact, there are very few examples of the new model binding in ASP.NET 4.5 using anything other than a couple of TextBox controls).
Can anybody shed any further light on the issue (and preferably tell me what needs to be done)?
Other questions on SO that I have looked at...
- Binding a DropDownList in ListView InsertItemTemplate throwing an error
- ASP.NET ListView with identical markup in EditItemTemplate and InsertItemTemplate
- Bind SelectedValue of ASP.net DropDownList to custom object
- Databinding to ASP.NET DropDownList list in ListView
All of these use the older style binding methods and not the new methods in 4.5
Thanks.