1

I am writing an application wherein I need to iterate through a ArrayList sent by the controller to the view using Viewdata.

The controller action:

public ActionResult ConfirmItemsList(string[] arr)
{
   // I generate ArrayList here , call it arrayLst

   ViewData["ItemsList"] = arrayLst;
}

Now I need to iterate this ViewData and display as unordered list in the view ( which is not strongly typed ), but am not able to get the syntax of for/foreach right.

I keep getting the error : foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'

Can some one please help me.

Thanks.

3 Answers 3

4

I think you are missing a cast. If you know it's an arrayList cast it as such when you pull it out of ViewData. Check out this link for more info.

foreach(item in (ViewData["ItemsList"] as ArrayList))
{
    doSomethingHere();
}
Sign up to request clarification or add additional context in comments.

8 Comments

I tried the following: // itemslist is of type ArrayList - non generic <pre> foreach(item in (ViewData["itemsList"] as ArrayList)) { } </pre> I get System.String[] as output in the list items.
Don't forget the type: foreach(var item in (ViewData["ItemsList"] as ArrayList<Item>))
@Martin Booth ArrayList don't take a type. It should work with my edit.
@Amit I have no idea what is in your ArrayList. You probably have to cast whatever you are pulling out of the ArrayList also. Are you able to use a generic collection like a List?
I traced the problem, the arraylist data is coming from another controller action, which tries to pass that arraylist as a paramter, but ( i guess ) since that arraylist goes out of scope, the parameter at the called controller action becomes null, so no data was being displayed in view. Can some one suggest a way to pass System.Collections.ArrayList data ( just simple collection of strings ) from one controller action as a parameter to another controller action?
|
0

Doesn't have to be array list, you can just pass in your array if thats what you want

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

Comments

0

I am new in .net, I am using MVC5, I have checked some .net example where people are doing Strongly type view to pass the data from controller to view. For that you should have a model where those parameter will set and then you can pass the data.

Suppose my model is User

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; }
}
}

Now my controller is Home and in the index page there have a login page

If you go with the traditional way then your controller will be look like

[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);
    }

And the corresponding view will be

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

    </tr>
}

But if you are thinking to do some customization Like if you don't want to follow this traditional model and you want to post anything from view page, and get it in controller and after perform some operation if you have a set of data and you want to pass the data in to view and show it in different place in view then you have to use ViewBag Or ViewData

Then the controller will be

 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();
    }

And the View will be

@{
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>  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.