This sample on the WebFormContrib mini-framework is on how to do a form post. If you’d like a particular sample on how to do something or if I’m missing something, please leave a comment.
Here’s how…
- Create a New Web Site of Use an Existing
- Download and reference the WebFormContrib.DLL
- Delete the CodeFile=”Default.aspx.cs” Inherits=”_Default” in the <%@ Page area
- Delete the actual Default.aspx.cs file attached to the Default.aspx page
- Create a new Class in the App_Code folder ( I’m calling mine DefaultPage )
- Inherit the DefaultPage from BasePage<SampleEmployeeView>
- Create a new Class file called SampleEmployeeView and make it look like this:
public class SampleEmployeeView
{
[Required("Id is Required.")]
public int Id { get; set; }
[Required("First Name is Required.")]
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
//Could use AutoMapper
public static SampleEmployeeView MapEmployeeToView(Employee employee)
{
var view = new SampleEmployeeView();
view.FirstName = employee.Name.FirstName;
view.LastName = employee.Name.LastName;
view.EmailAddress = employee.EmailAddress;
view.Id = employee.Id;
return view;
}
//Could use AutoMapper
public Employee MapToEmployee()
{
var emp = new Employee();
emp.Name.FirstName = FirstName;
emp.Name.LastName = LastName;
emp.EmailAddress = EmailAddress;
emp.Id = Id;
return emp;
}
}
Employee Class & Person Class look like this:
public class Employee
{
public Employee()
{
Name = new PersonName();
}
public int Id { get; set; }
public PersonName Name { get; set; }
public string EmailAddress { get; set; }
}
public class PersonName
{
public PersonName(){}
public PersonName(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public string FirstName { get; set; }
public string LastName { get; set; }
}
- In the DefaultPage Class create in Step 6, add the following code:
public class DefaultPage : BasePage<SampleEmployeeView>
{
private IRepository rep;
protected void Page_Load(object sender, EventArgs e)
{
rep = DI.CreateSampleRepository();
if(Page.IsPostBack)
return;
getEmployee();
}
private void getEmployee()
{
//Pass in ID here to load employee into view.
ViewModel = SampleEmployeeView.MapEmployeeToView(rep.GetEmployeeBy(ViewModel.Id));
}
protected void SaveEmployee(object sender, EventArgs e)
{
if (!ModelIsValid(ViewModel))
return;
rep.Save(ViewModel.MapToEmployee());
C<HtmlGenericControl>("successmsg").Visible = true;
}
}
Notice the ModelIsValid(ViewModel), which checks the validators set on the ViewModel. I considered using Castle Validators, but I preferred to not have any 3rd party references. I sometimes get annoyed when an open-source project has a lot of dependencies on other projects. Anyhow…
- Now we need to setup a dummy repository and service locator like this:
public class DI
{
public static IRepository CreateSampleRepository()
{
return new SampleRepository();
}
}
public class SampleRepository : IRepository
{
public Employee GetEmployeeBy(int employeeId)
{
//Get Employee from DB here.
return new Employee {EmailAddress = "test@test.com", Id = 123, Name = new PersonName("John", "Doe")};
}
public void Save(Employee employee)
{
//Save Employee here.
}
}
public interface IRepository
{
Employee GetEmployeeBy(int employeeId);
void Save(Employee employee);
}
- And finally the HTML portion of the site…the “cool” part:
<%@ Page Language="C#" AutoEventWireup="true" Inherits="DefaultPage" %>
<%@ Import Namespace="WebFormContrib.Helpers" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="successmsg" runat="server" enableviewstate="false" visible="false">Saved Successfully.</div>
<%=ErrorMessages.ToList("Important Message") %>
<p><%=Html<TextBox>(f=>f.FirstName).Label("First Name:<br/>") %></p>
<p><%=Html<TextBox>(f=>f.LastName).Label("Last Name:<br/>") %></p>
<p><%=Html<TextBox>(f=>f.EmailAddress).Label("Email Address:<br/>") %></p>
<p><%=Html<HiddenField>(f=>f.Id) %></p>
<p><asp:Button ID="bSave" runat="server" Text="Save" OnClick="SaveEmployee" /></p>
</div>
</form>
</body>
</html>
Finished view should look like this:
Download the sample project here.
Please comment if you have any questions or suggestions. Thanks for reading!