This is a migrated thread and some comments may be shown as answers.

[Solved] How do I return a collection of ViewModels?

0 Answers 73 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Dennis Gundersen
Top achievements
Rank 1
Dennis Gundersen asked on 04 Oct 2010, 04:47 AM
Hi

I get an error when using RadGrid with Ajax binding and EF 4. According to this link http://www.telerik.com/help/aspnet-mvc/telerik-ui-components-grid-troubleshooting.html#ServerError I need to use ViewModels instead of the normal entities when returning the GridModel since the RadGrid doesn't understand EF assiciations.
I.e. this doesn't work:
public ActionResult _AjaxBinding()
{
    return View(new GridModel(repository.GetPeople()));
}

I already use a ViewModel for editing my Person entities which looks like this:
public class PersonFormViewModel
{
    // Properties
    public Person Person { get; set; }
    public List<Status> Statuses { get; set; }
    public List<City> Cities { get; set; }
    public List<Country> Countries { get; set; }
    // Constructor
    public PersonFormViewModel(Person person)
    {
        Person = person;
        Statuses = PickLists.GetStatuses().ToList();
        Cities = PickLists.GetCities().ToList();
        Countries = PickLists.GetCountries().ToList();
    }
}

My edit action calls the view like this:
public ActionResult Edit(int id)
{
    Person person = repository.GetPerson(id);
    var viewModel = new PersonFormViewModel(person);
    return View(viewModel);
}

This work jus fine. However, I seem to be completely unable to wrap my brain around how to do this for a collection in the Ajax select action.
[GridAction]
public ActionResult _AjaxBinding()
{
   return View(new GridModel("GetPeople_As_ViewModel_Collection()"));
}

What would a "GetPeople_As_ViewModel_Collection()" look like?

Re
Dennis
Tags
General Discussions
Asked by
Dennis Gundersen
Top achievements
Rank 1
Share this question
or