Getting started - The easy way

Register the modelbinder


Method 1, setting the modelbinder as always on, using default settings:

Edit your Global.asax.cs file, and add the following to the Application_Start method:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    // Add the modelbinder as the default modelbinder:
    ModelBinders.Binders.DefaultBinder = new MvcTricks.RoundTripModelBinding.DefaultModelBinder();
}


Method 2, setting the modelbinder as always on, using secure settings, and specifying own encryption parameters:

Edit your Global.asax.cs file, and add the following to the Application_Start method:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    var config = new MvcTricks.RoundTripModelBinding.Configuration(
        StorageModes.CompressAndEncrypt,
            Encoding.Default.GetBytes("Lorem ipsum dolor sit amet amet."), // 32 bytes Key
            Encoding.Default.GetBytes("Donec tincidunt.") // 16 bytes IV
    );

    // Add the modelbinder as default:
    ModelBinders.Binders.DefaultBinder = new MvcTricks.RoundTripModelBinding.DefaultModelBinder(config);
}

See http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged.aspx for info on the AesManaged class.

Use the modelbinder

The model will automatically be filled with data, before it is returned to the action method.


Method 1, Using the modelbinder to persist the model, using a MvcForm extension:

<% using (Html.BeginForm().AppendRoundTripModel(ViewContext)) { %>
...
<% } %>


Method 2, Using the modelbinder to persist the model, using a HtmlHelper extension:

<% using (Html.BeginForm()) { %>
    <%= Html.RoundTripModel() %>
<% } %>

Last edited Mar 20, 2011 at 11:05 PM by KTW, version 5

Comments

No comments yet.