Error when passing additional ViewData to strongly typed Partial View
My partial view isn't being loaded. Please find my code snippets, I can't figure out what is wrong. Help please.
The error message is:
c:MvcUI\Views\Project\Details.ascx(42): error CS1950: The best overloaded Add method 'System.Web.Mvc.ViewDataDictionary.Add(System.Collections.Generic.KeyValuePair)' for the collection initializer
I am having In my view:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcUI.Models.ProjectModel>" %><%Html.RenderPartial("LabelsDetails", Model.Categories, new ViewDataDictionary{"labelsName", labelsName }); %>
My 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"/>
开发者_Go百科 </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>
My projectModel
public class Label
{
public String Name { get; set; }
public int ID { get; set; }
}
public List<Label> Categories { get; set; }
The syntax you are using to initialize the ViewDataDictionary
is wrong. You need two {
. Try like this:
<% Html.RenderPartial(
"LabelsDetails",
Model.Categories,
new ViewDataDictionary {{ "labelsName", labelsName }}
); %>
Dariv code for renderpartial was helpful but there was an error on the partial view inherits which can be found on
http://sokhanh03.spaces.live.com/blog/cns!78F088CCD824626B!832.entry
精彩评论