ASP.NET MVC 2 を使用するプロジェクトでカスタム モデル バインダーを作成しました。このモデル バインダーは、モデルの 2 つのフィールドのみをバインドします。
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);
}
}
標準の VS 単体テストを使用して、このモデル バインダーをテストするにはどうすればよいですか? グーグルで数時間を費やし、いくつかの例を見つけました(http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx)が、この例はMVC1用であり、MVC2を使用している場合は機能しません.
私はあなたの助けに感謝します。