views:

641

answers:

2

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?

+1  A: 

There is another overload for RenderPartial that will pass your model through.

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

How to access? Just like you normally would with any view:

<%= Model.MagicSauce %>
jfar
+3  A: 

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>
<% } %>
Darin Dimitrov
if I create a class with many properties, I guess I have to initialize that class in my controller, then in the view pass it to the user control?
mrblah
You initialize the class in the controller, pass is to the view as a model and the view renders the partial re-passing the model.
Darin Dimitrov