How do you pass through a collection with a partial MVC 2 view?

how do you pass through a collection with a MVC 2 partial view? I've seen an example where they used the syntax;

<% Html.RenderPartial("QuestionPartial", question); %>

      

this only goes through in one object of the question.

what if I want to pass a few questions in a partial view and, say, want to list them.

How would I get down to MULTIPLE questions?

+2


a source to share


3 answers


Since your partial view will usually be placed in another (main) view, you must strictly point your main view to a composite ViewData object that looks something like this:

public class MyViewData
{
    public string Interviewee { get; set }
    // Other fields here...
    public Question[] questions { get; set }
}

      

In your controller:

var viewData = new MyViewData;
// Populate viewData object with data here.

return View(myViewData);

      



and in your opinion:

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

      

Then use tvanfosson's advice on a partial view.

+1


a source


Instead of passing question

, why not go through a set of questions, for example List<QuestionType>

?



0


a source


Typically you have a property IEnumerable<Question>

as a property in your view model - in fact, it could be a list or an array of Question objects. To use it in a partial, just pass this property of the view model as the model for the partial. The partial must be strongly typed to be accepted IEnumerable<Question>

as a model.

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

      

Partial:

<%@ Page Language="C#"
         MasterPageFile="~/Views/Shared/Site.Master"
         Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Question>>" %>

      

0


a source







All Articles