I've wrote custom model binder in project, that uses ASP.NET MVC 2. This model binder bind just 2 fields of model:
public class TaskFormBinder : DefaultModelBinder
{
protected override void BindProperty(ControllerContext controllerContext,
ModelBindingContext bindingContext,
PropertyDescriptor propertyDescriptor)
{
if (propertyDescriptor.Name == "Type")
{
var value = bindingContext.ValueProvider.GetValue("Type");
var typeId = value.ConvertTo(typeof(int));
TaskType foundedType;
using (var nhSession = Domain.GetSession())
{
foundedType = nhSession.Get<TaskType>(typeId);
}
if (foundedType != null)
{
SetProperty(controllerContext, bindingContext, propertyDescriptor, foundedType);
}
else
{
AddModelBindError(bindingContext, propertyDescriptor);
}
return;
}
if (propertyDescriptor.Name == "Priority")
{ /* Other field binding ... */
return;
}
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
}
How can i test this model binder using standart VS unit testing? Spent some hours googling, find couple examples (http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx), but this examples is for MVC1, and dont work when using MVC2.
I appreciate your help.
ModelMetaData
on theModelBindingContext
. Without it, you get a nondescriptNullReferenceException
onBindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
. – Microhenry