2012-03-23 54 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