【问题标题】:How do I iterate through a list in my View?如何遍历我的视图中的列表?
【发布时间】:2010-11-13 00:25:33
【问题描述】:

在我看来,我正在从网络服务返回一些结果并将它们存储在会话变量中。

这里是会话存储方法:

//AreaSummary[] is a reference to the webservice reference gubbins

 public AreaSummary[] Results
        {
            get
            {
                if (this.Results != null)
                {
                    return this.Results;
                }
                else
                {
                    if (HttpContext.Current.Session["Results"] != null)
                    {
                        this.Results = 
                                   (AreaSummary[])HttpContext.Current.Session["Results"];
                                   return this.Results;
                    }
                    else
                    {
                        return null;
                    }
                }
            }

            set
            {
                this.Results= null;
                HttpContext.Current.Session["Results"] = value;
            }
        }

在控制器中,我将结果设置为此会话,以省去一次又一次推送请求(用于分页)。

所以我将会话设置为结果集:

this.mySess.SessionVarss.Results = myservice.SearchForX("stuff");

Results”和“SearchForX”都属于AreaSummary[]

ViewData["ResultSet"] = this.mySess.SessionVars.Results;

然后我将它作为ViewData 在控制器中传递而没有问题,并将databind 它传递给repeaterdatagrid(我得到)。

我的第一个想法是使用foreach 循环。

所以在我尝试的视图中:

<% foreach (var item in (ViewData["ResultSet"] as List<MyMVC.webservice.AreaSummary[]>))
   { %>    
            <li class="image">
                <img src="<%=item.MainImage.ImageUrl %>" />
            </li>
<% } %>

我确信这个错误对某些人来说是显而易见的,我通过隧道视野错过了它。

它编译正常,但因以下错误而死:

Object reference not set to an instance of an object.

以下作品:

<% 
   placelist.DataSource = ViewData["ResultSet"];
   placelist.DataBind();
%>
<asp:Repeater ID="productlist" runat="server" Visible="true">
 <ItemTemplate>
   <li class="image">
       <img src="<%# DataBinder.Eval(Container.DataItem, "MainImage.ImageUrl")%>" />
   </li>
 </ItemTemplate>
</asp:Repeater>

但我更喜欢使用foreach 循环,因为返回的一些项目是数组,而repeater 我最终会做更多的事情。

适当的解决方案

最终更好的解决方案是将数据传递给视图:

return View("index",SearchResults);

然后右击->add viewindexcreate strongly typed view,选择webservice.AreaSummarylist

【问题讨论】:

  • 您如何使用中继器和数据网格以及 ASP.NET MVC。我想更好的问题是,为什么?
  • 该问题旨在避免在 ASP.NET MVC 中使用中继器,这就是解决方案所允许的。添加一个愚蠢的问题,因为我确实指定我不想使用中继器方法。同样在 Wekeroad 上,显示这些 ASP.net 控件可以毫无问题地使用,他是对的,因为它们确实有效。但根据问题,我不想走那条路。

标签: .net asp.net-mvc .net-3.5 c#-3.0


【解决方案1】:

您的 for 循环对我来说看起来不错,尽管会话和视图数据并不完全相同。

视图数据存储在会话中,但直接存储在会话中的任何内容都必须从视图中的会话中引用。在您的情况下,您将值存储在 Session["Results"] 中,然后尝试在 ViewData["ResultSet"] 的视图中引用它。

下面的看起来也有点可疑。

public AreaSummary[] Results
    {
        get
        {
            if (this.Results != null)
            {
                return this.Results;
            }

这是一个递归调用,看起来有点不稳定。您在 getter 中引用了 Property getter,您是不是要引用一个字段?

【讨论】:

  • 在控件中,我将会话传递回视图... -- ViewData["ResultSet"] = this.mySess.SessionVarss.Results; -- 如果我使用数据中继器和数据绑定,它工作正常。
【解决方案2】:

我刚刚在另一个问题中发布了这个示例。试试这样的,

<table class="results" width="100%" border="0" cellpadding="5">
<thead class="tablehead">
  <tr> 
    <td width="55px"><b>Country</b></td>
    <td width="55px"><b>State</b></td>
    <td width="55px"><b>City</b></td>
    <td width="55px"><b>Town</b></td>
    <td width="55px"><b>Postal</b></td>
  </tr>
</thead>
<tbody>
<% 
    int count = 0;
    foreach (var item in (IEnumerable<MY_RESULTS>)ViewData["My_Results"])
    {
        if (count % 2 == 0) { Response.Write("<tr class='even'>"); } else { Response.Write("<tr class='odd'>"); }   
    %>
            <td><%= Html.Encode(item.Country)%></td>
            <td><%= Html.Encode(item.State)%></td>
            <td><%= Html.Encode(item.City)%></td>
            <td><%= Html.Encode(item.Town)%></td>
            <td><%= Html.Encode(item.Postal)%></td>
        </tr>

    <% count += 1; } %>
</tbody>
</table>

【讨论】:

  • 你我的好人非常接近目标。 foreach(var item in (IEnumerable)ViewData["Results"]) 它是关于 AreaSummary 不是 IEnumerable 的,所以这正是我需要的:o) 谢谢,不是我不必使用中继器
猜你喜欢
  • 2011-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
相关资源
最近更新 更多