views:

4236

answers:

4

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"

A: 

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

Chrisb
A: 

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
+6  A: 

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
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
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<ContactsView>" ... />. It seems like it probably has the inherit attribute doing something like: Inherits="System.Web.MVC.ViewPage<IEnumerable<something something>>"
MunkiPhD
+1 This is a really nice description. It's the only example I've seen that clearly shows all of the conventions.
Robert Harvey
Ok, let's say that ContactsList contains objects with 2 fields "Name" and "Count". How to acces them via View ?
Tony
A: 

Hm but what if I need to make the Model to be the System.Boolean type ?

If the View's header looks like:

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

the error appears:

CS0452: The type 'bool' must be a reference type in order to use it as parameter 'TModel' in the generic type or method 'System.Web.Mvc.ViewPage TModel>'

Do I really need to create a new custom class object which has a bool property inside ? (that scenario works, but I want to know if there is a faster solution, I don't wanna have a mess in my project)

Tony