【问题标题】:Iterating through ArrayList in ViewData ASP.NET MVC遍历 ViewData ASP.NET MVC 中的 ArrayList
【发布时间】:2011-07-25 18:43:51
【问题描述】:

我正在编写一个应用程序,其中我需要使用 Viewdata 遍历控制器发送到视图的 ArrayList。

控制器动作:

公共 ActionResult ConfirmItemsList(string[] arr) { // 我这里生成 ArrayList,叫它 arrayLst ViewData["ItemsList"] = arrayLst; }

现在我需要迭代这个 ViewData 并在视图中显示为无序列表(不是强类型),但我无法正确获取 for/foreach 的语法。

我不断收到错误消息:foreach 语句无法对“object”类型的变量进行操作,因为“object”不包含“GetEnumerator”的公共定义

谁能帮帮我。

谢谢。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-2


    【解决方案1】:

    我认为您缺少演员表。如果您知道它是一个 arrayList,那么当您将它从 ViewData 中拉出时,它就会被强制转换。 Check out this link for more info.

    foreach(item in (ViewData["ItemsList"] as ArrayList))
    {
        doSomethingHere();
    }
    

    【讨论】:

    • 我尝试了以下方法: // itemslist 是 ArrayList 类型 - 非通用
       foreach(item in (ViewData["itemsList"] as ArrayList)) { } 
      我得到 System .String[] 作为列表项中的输出。
    • 别忘了类型:foreach(var item in (ViewData["ItemsList"] as ArrayList))
    • @Martin Booth ArrayList 不采用类型。它应该适用于我的编辑。
    • @Amit 我不知道你的 ArrayList 中有什么。您可能还必须从 ArrayList 中提取任何内容。你能使用像 List 这样的通用集合吗?
    • 我追踪了这个问题,arraylist 数据来自另一个控制器操作,它试图将 arraylist 作为参数传递,但是(我猜)因为 arraylist 超出范围,参数在调用的控制器操作变为空,因此视图中没有显示任何数据。有人可以建议一种将 System.Collections.ArrayList 数据(只是简单的字符串集合)从一个控制器操作作为参数传递给另一个控制器操作的方法吗?
    【解决方案2】:

    不必是数组列表,如果你想要的话,你可以传入你的数组

    foreach(var item in (string[])ViewData["ItemsList"])
    {    
    }
    

    【讨论】:

      【解决方案3】:

      我是 .net 的新手,我正在使用 MVC5,我检查了一些 .net 示例,其中人们正在执行强类型视图以将数据从控制器传递到视图。为此,您应该有一个模型来设置这些参数,然后您可以传递数据。

      假设我的模型是用户

      namespace Mvc5Application.Models{
      using System;
      using System.Collections.Generic;
      
      public partial class User
      {
          public System.Guid UserID { get; set; }
          public string Username { get; set; }
          public string Password { get; set; }
          public Nullable<System.Guid> OrgID { get; set; }
      
          public virtual Organization Organization { get; set; }
      }
      }
      

      现在我的控制器是 Home 并且在索引页面中有一个登录页面

      如果您使用传统方式,那么您的控制器将看起来像

      [AllowAnonymous]
          public ActionResult Index()
          {
              ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
      
              return View();
          }
      
          [HttpPost]
          public ActionResult Index(User model)
          {
              ...[code for verification ]...                  
      
              return View(model);
          }
      

      而对应的视图将是

       @foreach (var item in Model)
      {
          <tr>
              <td>
                  @Html.DisplayFor(modelItem => item.username)
              </td>
              <td>
                  @Html.DisplayFor(modelItem => item.password)
              </td>
      
          </tr>
      }
      

      但是,如果您正在考虑进行一些自定义,例如,如果您不想遵循这种传统模式,并且想从视图页面发布任何内容,然后将其放入控制器并在执行某些操作后,如果您有一组数据并且您想将数据传递给查看并在视图中的不同位置显示它,那么您必须使用 ViewBag 或 ViewData

      那么控制器将是

       public ActionResult Index()
          {
              ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
      
              return View();
          }
      
          [HttpPost]
          public ActionResult Index(FormCollection form)
          {
              String Username = form["Username"].ToString();
              String Password = form["Password"].ToString();
              Dictionary<string, string> MyList = new Dictionary<String, String>();
              MyList.Add("name",Username);
              MyList.Add("pass",Password);
      
              ViewData["user"] = MyList;
      
      
              return View();
          }
      

      视图将是

      @{
      ViewBag.Title = "Home Page";
      var list = ViewData["user"] as Dictionary<String, String>; 
      }
      <h2>@ViewBag.Message</h2>
      <div>
      
      @if (list != null)
      {
          @Html.Label("User:-")  @list["name"]
      <br />@Html.Label("password:-") @list["pass"]  
      
      }
      </div>
      <p>
      Please ligin / Register from here</p>
      @using (Html.BeginForm("Home"))
      {
      <table>
          <tr>
              <td>User Name</td>
              <td>@Html.TextBox("Username", ViewBag.CurrentFilter as string)</td>
          </tr>
          <tr>
              <td>Password</td>
              <td>@Html.TextBox("Password", ViewBag.CurrentFilter as string)</td>
          </tr>
          <tr>
              <td></td>
              <td><input type="submit" value="Save" class="btn btn-default" /></td>
          </tr>
      
      </table>  
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-02
        • 2011-07-07
        • 1970-01-01
        • 1970-01-01
        • 2013-12-26
        • 2015-11-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多