ASP.NET MVC 5 ViewModel绑定

时间:2016-05-20 14:20:04

标签: c# asp.net-mvc asp.net-mvc-5 asp.net-mvc-viewmodel

我无法绑定控制器中ViewModel中的服务提取数据(事件)。 没有ViewModel就可以正常工作。

域名模型

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; }

    }
}

我只需要几个属性: 的视图模型

    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; }

        }
    }

现在在控制器我有这个(我省略了服务初始化部分 - 工作正常)。因此事件包含事件列表(来自域模型)。

        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);
        }
    }
}

基于ViewModel生成标准列表视图并获得:

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:现在正在使用控制器:

 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);
        }

更新2 - SHORTER和我更好地与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);
        }

1 个答案:

答案 0 :(得分:2)

您正在为自己添加一个集合:

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

那么,对于每个event您想要添加event?也许您打算将元素添加到modelList

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

(旁注:更清晰的命名策略可以帮助您更轻松地识别问题。modelList并没有真正告诉您它是什么。您可能正在使用events变量因为,在语义上,你应该处理事件。也许你可以称他们为domainEventsviewEvents?另外,命名Event类型的变量Event只是问对于问题。)