2

如何将集合传递给 MVC 2 局部视图?我看到了一个他们使用语法的例子;

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

这仅传递一个问题对象..

如果我想将几个问题传递到部分视图中,例如,我想将它们列出来怎么办。

我将如何传递几个问题?

4

3 回答 3

1

因为您的局部视图通常会放置在其他(主)视图中,所以您应该将主视图强输入到一个看起来像这样的复合 ViewData 对象:

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

在您的控制器中:

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

return View(myViewData);

在您看来:

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

然后在局部视图上使用tvanfosson 的建议。

于 2010-05-05T22:18:56.127 回答
0

question例如,为什么不通过一系列问题,而不是通过List<QuestionType>

于 2010-05-05T22:20:38.687 回答
0

通常,您IEnumerable<Question>的视图模型中有一个 as 属性——实际上它可能是一个列表或 Question 对象的数组。要在您的部分中使用它,只需将视图模型的该属性作为模型传递给部分。部分应该是强类型以接受一个IEnumerable<Question>作为它的模型。

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

部分的:

<%@ Page Language="C#"
         MasterPageFile="~/Views/Shared/Site.Master"
         Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Question>>" %>
于 2010-05-05T22:22:12.987 回答