Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 3 model binding with underscores

I'm posting json with variables names with underscores (like_this) and attempting to bind to a model that is camelcased (LikeThis), but the values are unable to be bound.

I know I could write a custom model binder, but since the underscored convention is so common I'd expect that a solution already existed.

The action/model I'm trying to post to is:

/* in controller */
[HttpPost]
public ActionResult UpdateArgLevel(UserArgLevelModel model) {
    // do something with the data
}

/* model */
public class UserArgLevelModel {
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string Surname { get; set; }
    public int ArgLevelId { get; set; }
}

and the json data is like:

{
    id: 420007,
    first_name: "Marc",
    surname: "Priddes",
    arg_level_id: 4
}

(Unfortunately I can't change either the naming of either the json or the model)

like image 611
Jordan Wallwork Avatar asked Jul 23 '12 13:07

Jordan Wallwork


1 Answers

You can start writing a custom Json.NET ContractResolver:

public class DeliminatorSeparatedPropertyNamesContractResolver :
    DefaultContractResolver
{
    private readonly string _separator;

    protected DeliminatorSeparatedPropertyNamesContractResolver(char separator)
        : base(true)
    {
        _separator = separator.ToString();
    }

    protected override string ResolvePropertyName(string propertyName)
    {
        var parts = new List<string>();
        var currentWord = new StringBuilder();

        foreach (var c in propertyName)
        {
            if (char.IsUpper(c) && currentWord.Length > 0)
            {
                parts.Add(currentWord.ToString());
                currentWord.Clear();
            }
            currentWord.Append(char.ToLower(c));
        }

        if (currentWord.Length > 0)
        {
            parts.Add(currentWord.ToString());
        }

        return string.Join(_separator, parts.ToArray());
    }
}

This is for your particular case, becase you need a snake case ContractResolver:

public class SnakeCasePropertyNamesContractResolver :
    DeliminatorSeparatedPropertyNamesContractResolver
{
    public SnakeCasePropertyNamesContractResolver() : base('_') { }
}

Then you can write a custom attribute to decorate your controller actions:

public class JsonFilterAttribute : ActionFilterAttribute
{
    public string Parameter { get; set; }
    public Type JsonDataType { get; set; }
    public JsonSerializerSettings Settings { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {    
        if (filterContext.HttpContext.Request.ContentType.Contains("application/json"))
        {
            string inputContent;
            using (var reader = new StreamReader(filterContext.HttpContext.Request.InputStream))
            {
                inputContent = reader.ReadToEnd();
            }

            var result = JsonConvert.DeserializeObject(inputContent, JsonDataType, Settings ?? new JsonSerializerSettings());
            filterContext.ActionParameters[Parameter] = result;
        }
    }
}

And finally:

[JsonFilter(Parameter = "model", JsonDataType = typeof(UserArgLevelModel), Settings = new JsonSerializerSettings { ContractResolver = new SnakeCasePropertyNamesContractResolver() })]
public ActionResult UpdateArgLevel(UserArgLevelModel model) {
{
    // model is deserialized correctly!
}
like image 79
Paolo Moretti Avatar answered Oct 06 '22 23:10

Paolo Moretti