Search code examples
asp.net-mvcrenderpartial

passing parameters to my partial view?


I am calling my partial view like this:

 <% Html.RenderPartial("~/controls/users.ascx"); %>

Can I pass parameters to partial view? How will I access them in the actual users.ascx page?


Solution

  • You could pass a model object to the partial (for example a list of strings):

    <% Html.RenderPartial("~/controls/users.ascx", new string[] { "foo", "bar" }); %>
    

    Then you strongly type the partial and the Model property will be of the appropriate type:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Collections.Generic.IEnumerable<string>>" %>
    
    <% foreach (var item in Model) { %>
        <div><%= Html.Encode(item) %></div>
    <% } %>