ASP.NET MVC 5 ViewModel Binding

Harvey

I cant bind data (events) fetch from service in ViewModel in controller. It works ok without ViewModel.

Domain model

namespace Tournament.Model
{
      public class Event
    {
        public int EventID { get; set; }
        public string Name { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public string Geolocation { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
        public string Description { get; set; }
        public string Type { get; set; }

        public virtual List<Team> Teams { get; set; }

    }
}

I need just few properties for: ViewModel

    namespace Tournament.Web.ViewModel.EventViews
    {
        public class EventIndexViewModel
        {
            public int EventIndexViewModelID { get; set; }
            public string Name { get; set; }
            public DateTime StartDate { get; set; }
            public DateTime EndDate { get; set; }
            public string City { get; set; }

        }
    }

And now in controller I have this (I ommited service initializing part - that works fine). So events hold list of Events (from domain model).

        public ActionResult Index()
        {
            //Create list of Events
            var events = eventService.GetEvents();

            //Search events

            //Add _eventService Event details to var event

            var model = new EventIndexViewModel();
            List<EventIndexViewModel> modelList = new List<EventIndexViewModel>();

            foreach (var Event in events)
            {
                events.Add(new Event {
                    Name = model.Name,
                    StartDate = model.StartDate,
                    EndDate = model.EndDate });
                    modelList.Add(model);
            }           

            return View(modelList);
        }
    }
}

Generated standard List view based on ViewModel and got:

Server Error in '/' Application.

Collection was modified; enumeration operation may not execute.

Source Error: 


Line 36:             List<EventIndexViewModel> modelList = new List<EventIndexViewModel>();
Line 37: 
**Line 38:             foreach (var Event in events)**
Line 39:             {
Line 40:                 events.Add(new Event {

UPDATE -SOLUTION: This is working controller now:

 public ActionResult Index()
        {
            //Create list of Events
            var domainEvents = eventService.GetEvents();


            //Add domain Events to ViewModel Event properties

            var eventViewModel = new EventIndexViewModel();
            List<EventIndexViewModel> eventListViewModel = new List<EventIndexViewModel>();

            foreach (var domainEvent in domainEvents)
            {
                eventListViewModel.Add(new EventIndexViewModel
                {    
                    Name = domainEvent.Name,
                    StartDate = domainEvent.StartDate,
                    EndDate = domainEvent.EndDate });
                }           

            return View(eventListViewModel);
        }

UPDATE 2 - SHORTER AND I GUESS BETTER WAY OF DOING IT WITH LINQ

public ActionResult Index()
        {
            //Create list of Events
            var domainEvents = eventService.GetEvents();

            //Search events

            //Add domain Event list to ViewModel list

                var eventListViewModel = domainEvents.Select(
                    x => new EventIndexViewModel()
                    {
                        Name = x.Name,
                        StartDate = x.StartDate,
                        EndDate = x.EndDate,
                        City = x.City

                    })
                        .ToList();

            return View(eventListViewModel);
        }
David

You're adding a collection to itself:

foreach (var Event in events)
{
    events.Add(new Event {
    //...

So, for every event you want to add an event? Perhaps you meant to add elements to modelList instead?

foreach (var Event in events)
{
    modelList.Add(new EventIndexViewModel {
    //...

(Side note: A cleaner naming strategy will help you identify problems more easily. modelList doesn't really tell you what it is. You're probably using the events variable because, semantically, you should be working with events. Maybe you can call them domainEvents and viewEvents? Also, naming a variable Event which is of type Event is just asking for problems.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related