Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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.