将其他ViewData传递给强类型的局部视图时出错

时间:2010-08-13 07:55:33

标签: asp.net-mvc

我的部分视图未加载。请找我的代码片段,我无法弄清楚是什么问题。请帮助。

错误消息是:

  

c:MvcUI \ Views \ Project \ Details.ascx(42):错误CS1950:集合初始化程序的最佳重载Add方法'System.Web.Mvc.ViewDataDictionary.Add(System.Collections.Generic.KeyValuePair)'

我在我看来:

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

我的部分视图

<%@ 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; }

2 个答案:

答案 0 :(得分:1)

用于初始化ViewDataDictionary的语法是错误的。你需要两个{。试试这样:

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

答案 1 :(得分:0)

renderpartial的Dariv代码很有帮助,但部分视图继承有错误,可以在

上找到

http://sokhanh03.spaces.live.com/blog/cns!78F088CCD824626B!832.entry