tags:

views:

52

answers:

2

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"/>
                    </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; }
+1  A: 

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 }}
); %>
Darin Dimitrov
On which line are you getting this exception?
Darin Dimitrov
it`s showing the error on <% Html.RenderPartial( "LabelsDetails", Model.Categories, new ViewDataDictionary {{ "labelsName", labelsName }} ); %>
Thanks, but now i`l having error message: c:\Code\MvcUI\Views\Project\LabelsDetails.ascx(7): error CS1061: MvcUI.Models.Label' does not contain a definition for 'Id' and no extension method 'Id' accepting a first argument of type 'MvcUI.Models.Label' could be found (are you missing a using – user281180
What's on line 7 in `c:\Code\MvcUI\Views\Project\LabelsDetails.ascx`?
Darin Dimitrov
Please find LabelsDetails above, as my partialview
Well it seems that you are calling `Model.ID` but your partial is strongly typed to `ProjectModel` and not `Label`, so there's no such property. By the way it is absolutely not clear how your model classes look like. What you've posted won't even compile as you've shown the `Label` class and after it a declaration of a `Categories` property which is not valid C#.
Darin Dimitrov
How can I correct that?
A: 

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