2012-03-23 149 views
1

我有一个具有显式属性的模型类,它似乎MVC活页夹不绑定它们。我得到错误MVC绑定显式属性

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary 

这是已知的问题吗?我无法在google上找到关于此的任何文档。

我班

public interface IPdf2Source 
    { 
     string Password { get; set; } 
     string OutputFormatSelected { get; set; } 
    } 

    public class OptionModel : IPdf2Source 
    { 

     public IPdf2Source Pdf2Source 
     { 
      get { return this; } 
     } 


     //Bind Ok 
     public string Email { get; set; } 


     //I get error on these properties while binding. 
     string IPdf2Source.Password { get; set; } 
     string IPdf2Source.OutputFormatSelected { get; set; } 
    } 

查看

@using (Html.BeginForm()) 
{ 
    @Html.TextBoxFor(p => p.Email) 
    @Html.TextBoxFor(p => p.Pdf2Source.Password) 
    @Html.HiddenFor(p=>p.Pdf2Source.OutputFormatSelected) 
} 

控制器动作,因为绑定失败这是永远不会被调用。如果我删除显式声明的属性,一切工作正常。

public JsonResult ValidateFile(OptionModel formData) 
{ 
} 
+0

显示一些代码。 – 2012-03-23 07:59:25

+0

我已更新帖子。 – Tomas 2012-03-23 08:01:46

+0

请在您的问题中显示一些代码,以便我们可以更好地了解您的问题。添加尽可能多的代码。 – 2012-03-23 08:02:30

回答

0
//I get error on these properties while binding. 
    string IPdf2Source.Password { get; set; } 
    string IPdf2Source.OutputFormatSelected { get; set; } 

为什么这些甚至在那里呢?这是不正确的语法。而且,您已经在界面中定义了这些值。

编辑

你是正确的,对不起,我有点过时显式接口定义的。它看起来像你几乎完美地做了几乎所有的事情,但现在我看着它,我认为我看到了你的问题。看起来您需要将您的显式定义更改为public

public string IPdf2Source.Password { get; set; } 
    public string IPdf2Source.OutputFormatSelected { get; set; } 
+0

你能解释一下这里不正确吗?这是明确的属性声明,我使用这个技巧在类中对属性进行分组。另外,如果我们在接口中有属性,我们需要它们在派生类中实现。 – Tomas 2012-03-23 09:42:23

+0

@Tomas - 请参阅编辑。 – 2012-03-23 09:54:14