Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View model_binder.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
namespace MyProject.Web.Binders{
using System.Web.Mvc;
using Domain;
public class RootBinder<TRoot,TRepository>:DefaultModelBinder
where TRepository:IRepository<TRoot> where TRoot:IRoot{
private readonly TRepository _repository;
public RootBinder(TRepository repository){
Guard.AgainstNull(repository);
_repository=repository;
}
public override object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext){
if(bindingContext.ModelType!=typeof(TRoot))
return base.BindModel(controllerContext,bindingContext);
var vp=bindingContext.ValueProvider;
var v=vp.GetValue(bindingContext.ModelName)??vp.GetValue("id");
var id=int.Parse(v.AttemptedValue);
return _repository.Get(id);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.