using System.Collections.Generic;
using Nancy.ModelBinding;
public class CustomerModule : NancyModule
Get["/index"] = parameters =>
var customers = GetCustomers();
Post["/add"] = parameters =>
var customer = this.Bind<Customer>();
var result = this.Validate<Customer>(customer);
return RenderErrorView(result);
var customers = GetCustomers();
private string RenderErrorView(ModelValidationResult result)
var url = string.Format("<a href='{0}'>Back to List</a>", this.Context.ToFullPath("~/index"));
var errorsDescription = result.Errors.SelectMany(e => e.Value.Select(error => error.ErrorMessage)).ToList();
return "Sorry, but your data is invalid. " + url + "<br/>Errors: <br/>" + string.Join(", <br/>", errorsDescription);
private List<Customer> GetCustomers()
return new List<Customer>()
{Id = 1, Name = "John"}, new Customer()
{Id = 2, Name = "Bob"}, new Customer()
{Id = 42, Name = "Alice"}, };