3

I would like bind an array data to view in ASP.NET MVC, how can I do that?

sorry for not clear about my question. Right now, I creat a custom object(not array), I tried to pass it to View, but the error shows

"The model item passed into the dictionary is of type 'ContactView' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable"

Smallville
  • 685
  • 4
  • 13
  • 24
  • Eric, the error message is saying exactly what is wrong. You are passing it a custom object (not an array), but it is expecting a generic collection of objects. – Robert Harvey Jun 05 '09 at 17:27

3 Answers3

6

You can use the ViewData[], but the best way would be to actually create a view class and return that.

So let's say the view you want to send the data is called Contacts. Create a new View class and return that. so instead of this:

public ActionResult Contacts(){
    ViewData["contacts"] = arrayOfContacts[];
    ...
    return View();
}

You can get strongly-typed views by doing this:

public class ContactsView(){
     Object[] ContactsList {get;set;}
}

public ActionResult Contacts(){
    ...
    return View(new ContactsView(){
        ContactsList = arrayOfContacts[];
    });
}

Then in the actual view, you can have it be strongly typed by accepting objects of type ContactsView. That way in the actual View, have it inherit like so:

... Inherits="System.Web.Mvc.ViewPage<ContactsView>" ...

Which allows you to call your array like...

Model.ContactsList

as opposed to this:

 object[] arrayOfItems = (Object[])ViewData["theContactsList"];

In which case you'd probably want to check if it's not null, etc. The benefit of this is that if you refactor it's much easier. Not to mention the ease and type security of use of strongly typed objects.

MunkiPhD
  • 3,636
  • 1
  • 29
  • 51
  • sorry for not clear about my question. Right now, I creat a custom object, I tried to pass it to View, but the error shows "The model item passed into the dictionary is of type 'ContactView' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable" – Smallville May 15 '09 at 15:07
  • It seems like when you created the page you told it to be strongly typed of some type of IEnumerable (i.e. a list, dictionary, etc). What does the very first line of your View say? It should be something like <% Page="... ... " Inherits="System.Web.MVC.ViewPage" ... />. It seems like it probably has the inherit attribute doing something like: Inherits="System.Web.MVC.ViewPage>" – MunkiPhD May 15 '09 at 16:20
  • +1 This is a really nice description. It's the only example I've seen that clearly shows all of the conventions. – Robert Harvey Jun 05 '09 at 17:20
  • Ok, let's say that ContactsList contains objects with 2 fields "Name" and "Count". How to acces them via View ? – Tony Sep 13 '10 at 19:24
0

Put it in the ViewData, and use for..each to loop through it?

Chrisb
  • 729
  • 1
  • 5
  • 11
0

Look at this link: http://nerddinnerbook.s3.amazonaws.com/Part6.htm

Basically you could load the array into a viewdata object, and use that as you like.

Bravax
  • 10,453
  • 7
  • 40
  • 68