Asp.net mvc 将其他ViewData传递到强类型局部视图时出错

Asp.net mvc 将其他ViewData传递到强类型局部视图时出错,asp.net-mvc,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.Vi

未加载我的局部视图。请找到我的代码片段,我不知道出了什么问题。请帮忙

错误消息是:

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>


  • 我的项目模型

    public class Label
    {
        public String Name { get; set; }   
        public int ID { get; set; }
    }
     public List<Label> Categories { get; set; }
    
    公共类标签
    {
    公共字符串名称{get;set;}
    公共int ID{get;set;}
    }
    公共列表类别{get;set;}
    
    用于初始化
    ViewDataDictionary
    的语法错误。您需要两个
    {
    。请这样尝试:

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

    用于renderpartial的Dariv代码很有帮助,但是部分视图继承上有一个错误,可以在上找到


    您在哪一行收到此异常?它在“谢谢”上显示了错误,但现在我收到了错误消息:c:\Code\MvcUI\Views\Project\LabelsDetails.ascx(7):错误CS1061:MvcUI.Models.Label'不包含“Id”的定义,并且找不到接受“MvcUI.Models.Label”类型的第一个参数的扩展方法“Id”(您是否缺少一个using–USER281180C:\code\MvcUI\Views\Project\LabelsDetails.ascx中第7行的内容??请查找上面的LabelsDetails,如我的partialview所示