Skip to content

ktw/MvcTricks.RoundTripModelBinding

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MvcTricks.RoundTripModelBinding

Utility for persisting state on MVC pages.
This is an effort to simplify model binding, by automatically persisting the the model data, which is else lost when sending data to and from the client.
It is not an attempt to reinvent asp.net viewstate!
The main purpose is to avoid using a bunch of hidden values in views, for id's, collections and other values, which might even come from database queries.


The current release uses ServiceStack.Text internally, which is the fastest and most compact text-based serializer for .NET.
Currently these types have their own serialization methods implemented:

  • System.Net.Mail.MailAddress
  • System.Net.IPAddress

Warning!

Do not use it on classes which are lazy loadable, like NHibernate entities.
Bad things will happen.


Getting started - The easy way

Download the package from NuGet

Search for "MvcTricks.RoundTripModelBinding" in your IDE, or run this packagemanager command:
PM> Install-Package MvcTricks.RoundTripModelBinding

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);

    // Specify the default storage mode, and encryption keys
    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.

Register custom serialization implementations

Method 1, using delegates:

protected void Application_Start()
{
    // After the setup code ...
	MvcTricks.RoundTripModelBinding.Configuration.RegisterSerializationHandlerFor<MyCustomType>(
		x => { return x.ToString(); }, 
        x => { return MyCustomType.Parse(x); }
	);
}

Method 2, implementing the MvcTricks.RoundTripModelBinding.Serialization.ISerializationHandler<T> interface:
// Custom implementation:
public class MyCustomTypeSerializationHandler : MvcTricks.RoundTripModelBinding.Serialization.ISerializationHandler<MyCustomType>
{
public string Serialize(MyCustomType value)
{
	return value.ToString();
}

public MyCustomType Deserialize(string value)
{
	return MyCustomType.Parse(x);
}

}

protected void Application_Start() { // ... Setup code ... MvcTricks.RoundTripModelBinding.Configuration.RegisterSerializationHandlerFor<MyCustomType>(new MyCustomTypeSerializationHandler()); }


Use the modelbinder

The model will automatically be filled with data, before it is returned to the action method.
s 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() %>
<% } %>

Method 3, Using the modelbinder to persist any object, using a HtmlHelper extension, and extract it manually from the controller:
<% using (Html.BeginForm()) { %>
    <%= Html.RoundTripModelFor("key_to_identify_value", myObject) %>
<% } %>

And from the controller:
public class MyController : Controller
{
[HttpPost]
public ActionResult MyAction(FormCollection form)
{
	MyObject otherViewModel = this.GetRoundTripModel&lt;MyObject&gt;("key_to_identify_value");
}

}

About

Utility for persisting state on MVC pages.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages