2010-08-13 71 views
0

我的局部視圖未加載。請找到我的代碼片段,我無法弄清楚什麼是錯的。請幫助。將其他ViewData傳遞給強類型局部視圖時出錯

的錯誤信息是:

C:MvcUI \視圖\項目\ Details.ascx(42):錯誤CS1950:最好重載Add方法「System.Web.Mvc.ViewDataDictionary.Add(系統.Collections.Generic.KeyValuePair)」的集合初始化

我有在我看來:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcUI.Models.ProjectModel>" %><%Html.RenderPartial("LabelsDetails", Model.Categories, new ViewDataDictionary{"labelsName", labelsName }); %> 

我partialView

<%@ Control language="C#"Inherits="System.Web.Mvc.ViewUserControl<Models.ProjectModel>" %> <%int count = 0; %> 
<%var labelsName = ViewData["labelsName"];%> 

      <%if (Model!=null) {%>    

       <%if (!String.IsNullOrEmpty(Model.Name))     
        {%> 
        <li> 
          <%: Html.Hidden((labelsName)+".Index", count.ToString())%> 
          <%: Html.TextBox((labelsName)+"[" + (count) + "].Name", Model.Name, new { Style = "width:280px" })%> 
          <%: Html.Hidden((labelsName)+"[" + (count++) + "].ID", Model.ID, new { Style = "width:280px" })%> 
         <input type="button" value = "Delete"/> 
        </li> 
        <%} 
        %> 
       <%} %> 

       <li> 
        <%: Html.Hidden((labelsName) + ".Index", count.ToString())%> 
          <%: Html.TextBox((labelsName) + "[" + (count) + "].Name", "", new { Style = "width:280px" })%> 
          <%: Html.Hidden((labelsName) + "[" + (count++) + "].ID", 0, new { Style = "width:280px" })%> 
        <input type="button" value= "Add" /> 
       </li> 

我projectModel

public class Label 
{ 
    public String Name { get; set; } 
    public int ID { get; set; } 
} 
public List<Label> Categories { get; set; } 

回答

1

語法您使用初始化ViewDataDictionary是錯誤的。你需要兩個{。試試這樣:

<% Html.RenderPartial(
    "LabelsDetails", 
    Model.Categories, 
    new ViewDataDictionary {{ "labelsName", labelsName }} 
); %> 
+0

被發現在這行你得到這個例外? – 2010-08-13 08:39:43

+0

它顯示<%Html.RenderPartial上的錯誤( 「LabelsDetails」, Model.Categories, new ViewDataDictionary {{「labelsName」,labelsName}} ); %> – learning 2010-08-13 08:46:15

+0

謝謝,但現在我有錯誤信息:c:\ Code \ MvcUI \ Views \ Project \ LabelsDetails.ascx(7):錯誤CS1061:MvcUI.Models.Label'不包含'Id'並沒有擴展方法'Id'接受'MvcUI.Models.Label'類型的第一個參數可以找到(你是否缺少一個使用 - user281180 – learning 2010-08-13 08:48:53

相關問題