I am trying to write a custom model binder but I'm having great difficulty trying to figure how to bind complex composite objects.
this is the class I'm trying to bind to:
public class Fund
{
public int Id { get; set; }
public string Name { get; set; }
public List<FundAllocation> FundAllocations { get; set; }
}
and this is how my attempt at writing the custom binder looks like:
public class FundModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
throw new NotImplementedException();
}
public object GetValue(ControllerContext controllerContext, string modelName, Type modelType, ModelStateDictionary modelState)
{
var fund = new Fund();
fund.Id = int.Parse(controllerContext.HttpContext.Request.Form["Id"]);
fund.Name = controllerContext.HttpContext.Request.Form["Name"];
//i don't know how to bind to the list property :(
fund.FundItems[0].Catalogue.Id = controllerContext.HttpContext.Request.Form["FundItem.Catalogue.Id"];
return fund;
}
}
Any Ideas
thanks Tony
See Question&Answers more detail:os