Skip to content

Commit

Permalink
Adding an attribute that is common in implementations.
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldotknopf committed Jan 15, 2020
1 parent 7cf73a9 commit 30fea75
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Statik.Mvc/FormRouteDataAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace Statik.Mvc
{
public class FromRouteDataAttribute : Attribute, IBinderTypeProviderMetadata, IBindingSourceMetadata
{
public FromRouteDataAttribute()
{
BinderType = typeof(Binder);
}

public BindingSource BindingSource => BindingSource.Custom;

public Type BinderType { get; }

private class Binder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
var routeData = bindingContext.ActionContext.RouteData.Values;
bindingContext.Result = ModelBindingResult.Success(routeData[bindingContext.FieldName]);
return Task.CompletedTask;
}
}
}
}

0 comments on commit 30fea75

Please sign in to comment.