2011-03-30 6 views
1

컨트롤러에서 ViewData를 사용하여 뷰에 보낸 ArrayList를 반복해야하는 응용 프로그램을 작성 중입니다. ViewData에서 ArrayList를 반복 ASP.NET MVC

컨트롤러 조치 :

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

    ViewData["ItemsList"] = arrayLst; 
} 

지금 나는이을 ViewData를 반복하고 (강력하게 형식화되지 않음)보기로 정렬되지 않은 목록을 표시해야하지만/foreach 문에 대한 구문을 얻을 수 없습니다입니다 권리. '개체' 'GetEnumerator를'어떤 사람이 나를 도와 주시겠습니까

에 대한 공공의 정의가 포함되어 있지 않기 때문에 유형 '객체의 변수를 조작 할 수 없습니다 foreach 문 :

나는 오류가 계속.

감사합니다.

답변

4

나는 캐스트가 누락 된 것 같습니다. ArrayList라는 것을 알고 있으면 ViewData에서 꺼내는 것과 같이 캐스트합니다. Check out this link for more info.

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

다음을 시도 : // itemslist는 ArrayList 유형입니다 - 일반이 아닌

 foreach(item in (ViewData["itemsList"] as ArrayList)) { } 
목록 항목에 출력으로 System.String []이 표시됩니다. – Jake

+0

타입을 잊지 마라. foreach (ViewData [ "ItemsList"] in ArrayList )) –

+0

@Martin Booth ArrayList는 타입을 취하지 않는다. 그것은 내 편집 작업을해야합니다. – Pete

0

당신은 내가, 내가 MVC5를 사용하고 .NET에서 새로운 오전

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

를 원하는 이잖아, 내가 확인 한 경우 배열 목록을 할 필요가 없습니다, 당신은 당신의 배열에 전달할 수 있습니다 사람들이하는 몇 가지 .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; } 
} 
} 

입니다

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

그리고 해당보기가 될 것입니다

,958,

그러나 기존의 모델을 따르고 싶지 않고보기 페이지에서 아무 것도 게시하지 않고 컨트롤러로 가져오고 설정이있는 경우 일부 작업을 수행 한 것처럼 일부 사용자 정의 작업을 수행하려는 경우 당신이보기에 데이터를 전달하고보기에 서로 다른 장소에 표시 할 데이터의 당신은 그런 컨트롤러가

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 또는을 ViewData

를 사용할 필요가 그리고보기

될 것입니다
@{ 
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> 
}