Search code examples
asp.net-mvctimestampversion

ASP.NET MVC: dealing with Version field


I have a versioned model:

public class VersionedModel
{
    public Binary Version { get; set; }
}

Rendered using

<%= Html.Hidden("Version") %>

it gives:

<input id="Version" name="Version" type="hidden" value="&quot;AQID&quot;" />

that looks a bit strange. Any way, when the form submitted, the Version field is always null.

public ActionResult VersionedUpdate(VersionedModel data)
{ 
    ...
}

How can I pass Version over the wire?

EDIT:

A naive solution is:

public ActionResult VersionedUpdate(VersionedModel data)
{ 
    data.Version = GetBinaryValue("Version");
}

private Binary GetBinaryValue(string name)
{
    return new Binary(Convert.FromBase64String(this.Request[name].Replace("\"", "")));
}

Solution

  • This post http://forums.asp.net/p/1401113/3032737.aspx#3032737 suggests to use LinqBinaryModelBinder from http://aspnet.codeplex.com/SourceControl/changeset/view/21528#338524.

    Once registered

    protected void Application_Start()
    {
        ModelBinders.Binders.Add(typeof(Binary), new LinqBinaryModelBinder());
    }
    

    the binder will automatically deserialize Version field

    public ActionResult VersionedUpdate(VersionedModel data) 
    { ... }
    

    rendered this way:

    <%= Html.Hidden("Version") %>
    

    (See also http://stephenwalther.com/blog/archive/2009/02/25/asp.net-mvc-tip-49-use-the-linqbinarymodelbinder-in-your.aspx)