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>
<%}
%>
<%} %>
<% } %>
Instead of using the ViewDataDictionary can’t you simply add the required values to the model:
and then simply:
Other than that there’s nothing wrong with your syntax. The error you are getting is from somewhere else:
UPDATE:
Now that you’ve shown your code, let me suggest you a cleaner approach using editor templates.
Start by defining a model:
Then a controller which will fill this model:
Then the view (
~/Views/Home/Index.aspx):and finally the editor template (
~/Views/Home/EditorTemplates/Label.ascx):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.