tags:

views:

51

answers:

1

I would like to pass more than one parameter to the partial view. With the following

 <% Html.RenderPartial("Details", Model.test, new ViewDataDictionary { { "labelName", "Values1" }, {"header", "Header1"}, {"header2", "Header2"}}); %> 

code, I am having error message

) missing.

What is wrong?


<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<MvcUI.Models.Label>>" %>

 <%var label = ViewData["labelName"];%>
 <%int count = 0; %>


            <%if (Model!=null) {%>           
               <% foreach (var model in Model){ %>     


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

                <% } %> 
+1  A: 

Instead of using the ViewDataDictionary can't you simply add the required values to the model:

public class MyModel
{
    public string Test { get; set; }
    public string LabelName { get; set; }
    public string Header { get; set; }
    public string Header2 { get; set; }
}

and then simply:

<% Html.RenderPartial("Details", Model); %> 

Other than that there's nothing wrong with your syntax. The error you are getting is from somewhere else:

<% Html.RenderPartial(
    "Details", 
    Model.test, 
    new ViewDataDictionary { 
        { "labelName", "Values1" }, 
        { "header", "Header1" }, 
        { "header2", "Header2" }
    }
); %>

UPDATE:

Now that you've shown your code, let me suggest you a cleaner approach using editor templates.

Start by defining a model:

public class MyModel
{
    public IEnumerable<Label> Labels { get; set; }
}

public class Label
{
    public string ID { get; set; }
    public string Name { get; set; }
}

Then a controller which will fill this model:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyModel
        {
            Labels = new[] 
            {
                new Label { ID = "l1", Name = "Label 1" },
                new Label { ID = "l2", Name = "Label 2" },
                new Label { ID = "l3", Name = "Label 3" }
            }
        };
        return View(model);
    }
}

Then the view (~/Views/Home/Index.aspx):

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SomeNs.Models.MyModel>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <ul>
        <%: Html.EditorFor(x => x.Labels) %>
    </ul>
</asp:Content>

and finally the editor template (~/Views/Home/EditorTemplates/Label.ascx):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SomeNs.Models.Label>" %>
<li>
    <%: Html.HiddenFor(x => x.ID) %>
    <%: Html.TextBoxFor(x => x.Name) %>
    <input type="button" value="Delete" />
</li>

As you can see, using editor templates you no longer have to worry about naming the inputs, maintaining and incrementing indexes, writing loops, all those error prone things are handled by the framework automagically.

Darin Dimitrov
I`m still having the error message:c:\Dev\MvcUI\Views\Project\Details.ascx(5): error CS1026: ) expected
Once I pass only one parameter it works fine with the following:<% Html.RenderPartial( "Details", Model.test, new ViewDataDictionary { { "labelName", "Values1" } } ); %> !
@user281180, The error is on line 5 of `Details.ascx` and not the way you include the partial. What do you have on this line?
Darin Dimitrov
Please see in the code part, details added
Still good. Works fine on my PC. There must be something else you haven't shown.
Darin Dimitrov
where do u call Label.ascx from index.aspx?
Strange, but i`m still having the error message!
It's automatically called because of the name. In your page you do <%: `Html.EditorFor(x => x.Labels) %>` and MVC sees that the `Labels` property is an array of `Label` and checks if there's a file called `Label.ascx` in the `EditorTemplates` folder and applies this partial to each element of the list.
Darin Dimitrov
Ok, that`s great! thanks a lot!but what if i`m having: var model = new MyModel { Labels = new[] { ... } Labels2 = new[] { ... } Labels3 = new[] { ... } }; return View(model);
I have worked it this way as I am having multiples instances of Label