相关疑难解决方法(0)

ASP.NET MVC 2中的单元测试自定义模型绑定器

我在项目中编写了自定义模型绑定器,它使用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);
    }
}
Run Code Online (Sandbox Code Playgroud)

如何使用标准VS单元测试来测试此模型绑定器?花了几个小时谷歌搜索,找到几个例子( …

asp.net-mvc unit-testing modelbinders

21
推荐指数
1
解决办法
5122
查看次数

ValueProvider不包含TryGetValue的定义

在我的应用程序中,我试图从DateTime字段拆分日期和时间,以便我可以在日期上放置一个jQuery日期选择器.我找到了Hanselman用于拆分DateTime的代码,但是我遇到了编译错误bindingContext.ValueProvider.TryGetValue(modelName, out valueResult);.我得到的错误是:

错误3'System.Web.Mvc.IValueProvider'不包含'TryGetValue'的定义,并且没有可以找到接受类型'System.Web.Mvc.IValueProvider'的第一个参数的扩展方法'TryGetValue'(你错过了吗? using指令或程序集引用?)C:\ Documents and Settings\xxx\My Documents\Visual Studio 2008\Projects\MyProject\Project\Helpers\DateAndTimeModelBinder.cs 83 42项目

我错过了什么?我创建了一个新类,并将他的代码放在我项目的Helpers文件夹中.

c# asp.net-mvc datetime modelbinders

9
推荐指数
2
解决办法
6179
查看次数

标签 统计

asp.net-mvc ×2

modelbinders ×2

c# ×1

datetime ×1

unit-testing ×1