ASP.NET MVC How to Link a Custom Model to View

I would like to bind array data for viewing in ASP.NET MVC, how can I do this?

Sorry for my incomprehensible question. Right now I'm creating a custom object (not an array), I tried passing it to the View, but the error shows

"The model element passed to the dictionary is of type ContactView, but this dictionary requires a model element of type System.Collections.Generic.IEnumerable

+3
source share
3 answers

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

, , , , "". View . :

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

, :

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

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

, ContactsView. , :

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

, ...

Model.ContactsList

:

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

, , , .. , . .

+6

ViewData ... ?

0

Source: https://habr.com/ru/post/1708415/


All Articles