public
Created

  • Download Gist
model_binder.cs
C#
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);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.