I am current testing out model binding in ASP.NET 4.5.
I was testing a formview that accepts data from user and displays the list of data in a GridView below after it was successfully saved. When the data is saved to the list the gridview remained empty.
I tried this example by using an ObjectDataSource and it worked correct where by after the Postback when the form data is save the grid view shows all the items in the List.
Am I doing something that prevent from getting the desired behavior when using the Model Binding? If so what is the correct way to get the desired behavior?
ASP.NET markup of the formview and the Grid view
<asp:FormView ID="FormView1" runat="server" DefaultMode="Insert" ItemType="WebApplication35.Person" InsertMethod="SavePerson" SelectMethod="GetPeople">
<HeaderTemplate>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
</HeaderTemplate>
<InsertItemTemplate>
FirstName:
<asp:TextBox ID="FirstNameTextBox" runat="server" Text='<%# BindItem.FirstName %>' />
<br />
LastName:
<asp:TextBox ID="LastNameTextBox" runat="server" Text='<%# BindItem.LastName %>' />
<br />
Age:
<asp:TextBox ID="AgeTextBox" runat="server" Text='<%# BindItem.Age %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
</asp:FormView>
<asp:GridView ID="GridView1" runat="server" ItemType="WebApplication35.Person" SelectMethod="GetPeople" PageSize="2">
</asp:GridView>
Code behind
public partial class WebForm1 : System.Web.UI.Page
{
private static List<Person> people = new List<Person>();
public IEnumerable<Person> GetPeople()
{
return people;
}
public void SavePerson(Person person)
{
people.Add(person);
}
}
Person class
public class Person
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public int Age { get; set; }
}