c# - asp.net mvc 自定义模型绑定(bind)

标签 c# asp.net-mvc-2 binding model

您好,我正在使用带有 asp.net mvc 2.0 的自定义模型绑定(bind)器,一切都在本地运行,但是当部署到运行 iis 7 的服务器时,我收到一个奇怪的错误,很难获取有关错误的信息,如下所示,我我还附加了我的模型绑定(bind)类

错误:

[MissingMethodException: Method not found: 'System.Collections.Generic.IDictionary`2<System.String,System.Web.Mvc.ValueProviderResult> System.Web.Mvc.ModelBindingContext.get_ValueProvider()'.]
FID.Binders.documentModelBinder.GetValue(ModelBindingContext bindingContext, String key) in C:\Users\Bich Vu\Documents\Visual Studio 2010\Projects\FID\FID\Binders\DocumentModelBinder.cs:155
FID.Binders.documentModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) in C:\Users\Bich Vu\Documents\Visual Studio 2010\Projects\FID\FID\Binders\DocumentModelBinder.cs:61
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +475
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +152
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +709
System.Web.Mvc.Controller.ExecuteCore() +162
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +58
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371  Below  is the code file

代码:

public class BinddocumentAttribute : DataBindAttribute
{
  public  BinddocumentAttribute() : base(typeof(documentModelBinder)) { }

  public class documentModelBinder : DefaultModelBinder
  {
    ICategoryService _CategoryService=   ServiceLocator.Current.GetInstance<ICategoryService>();
    IFilePersistor _persistor = ServiceLocator.Current.GetInstance<IFilePersistor>();
    IFileTypeService _FiletypeService = ServiceLocator.Current.GetInstance<IFileTypeService>();      

    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var doc = base.BindModel(controllerContext, bindingContext) as Document;

        string key = bindingContext.ModelName;

        var errors = Core.xval.DataAnnotationsValidationRunner.GetErrors(doc);

        try
        {
            if (errors.Any())
                throw new xVal.ServerSide.RulesException(errors);

          foreach (string inputTagName in controllerContext.HttpContext.Request.Files)
          {
            HttpPostedFileBase filebase = controllerContext.HttpContext.Request.Files[inputTagName];
            if (filebase.ContentLength==0)
                 break; 
            string  extension= Path.GetExtension(filebase.FileName);

            if (!filebase.ContentType.Contains("image/"))
            {
              if (extension != _FiletypeService.GetFiletype(GetValue<short>(bindingContext, "Filetype_id")).extension.Trim())
              {
                bindingContext.ModelState.AddModelError("filetype", "Verify that the file type matches the selected file type");                           
                throw new RulesException("filetype", "Verify that the file type matches the selected file type", doc);

              }
            }
          }
        }
        catch (xVal.ServerSide.RulesException ex)
        {
            return null;
        }

        doc.Category1 = _CategoryService.GetCategory(GetValue<int>(bindingContext, "cat.parent_id"));
        doc.FileType1 = _FiletypeService.GetFiletype(GetValue<short>(bindingContext, "Filetype_id"));
        doc.modifieddate = DateTime.Now;

        if (doc.IsNewDocument)
        {
            doc.CreateParentFolders();
            doc.createdate = DateTime.Now;
        }

        UpdateFiles(controllerContext, doc);
        return doc;
    }

    private void UpdateFiles(ControllerContext controllerContext, Document doc)
    {
      foreach (string inputTagName in controllerContext.HttpContext.Request.Files)
      {
        HttpPostedFileBase filebase = controllerContext.HttpContext.Request.Files[inputTagName];
        if (filebase.ContentLength > 0)
        {
          if (filebase.ContentType.Contains("image/"))
          {
            Thumb image = new Thumb { type = filebase.ContentType, name = filebase.FileName, PostedFile = filebase, AssociatedDocument = doc, document_id=doc.document_id };
            image.filepath = _persistor.PersistFile(image);
            doc.Thumbs.Add(image);
          }
          else
          {
            doc.PostedFile = filebase;
            doc.filesize = long.Parse(filebase.ContentLength.ToString());
            doc.filepath = _persistor.PersistFile(doc);
          }
        }
      }
    }

    private T GetValue<T>(ModelBindingContext bindingContext, string key)
    {
        ValueProviderResult valueResult;
        bindingContext.ValueProvider.TryGetValue(key, out valueResult);
        bindingContext.ModelState.SetModelValue(key, valueResult);
        return (T)valueResult.ConvertTo(typeof(T));
    }

    protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var document = bindingContext.Model as Document;
        if (String.IsNullOrEmpty(document.title))
        {
            bindingContext.ModelState.AddModelError("Name", "...");
        }
    }
}

最佳答案

首先,您应该确保运行正确的程序集

第二 在 MVC2 中,以下内容无效。 IValueProvider 接口(interface)已更改

bindingContext.ValueProvider.TryGetValue(key, out valueResult); 

应替换为

valueResult = bindingContext.ValueProvider.GetValue(key); 

关于c# - asp.net mvc 自定义模型绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2738442/

相关文章:

wpf - 为什么绑定(bind)会影响高度?

wpf - 无法将 View 绑定(bind)到 ViewModel

android - MvvmCross Android - 按钮命令的relativesource绑定(bind)的替代方案

c# - 在Ilist中使用Func,为什么是lambda表达式?

c# - 如何在 ASP.NET MVC 单元测试中从 Moq 中的模拟返回中检索参数值

nhibernate - ASP.NET MVC 2 RC 模型与 NHibernate 和下拉列表绑定(bind)

asp.net-mvc - Asp.net mvc route 在通配符末尾删除数字文件夹

.net - 错误请求 - 无效 URL

c# - 如何将此 LINQ 查询编写为 Lambda 表达式?

c# - 自动开始 Storyboard (C#)