ASP.NET MVC LINQ:在 Action 中加入另一个模型

问题描述

我想将另一个模型加入我当前的 MVC 操作中,以便能够读取视图中的外键。我想将“ProductiveUnit”模型加入到下面的操作中,其中 ProductiveUnit 被加入到 ProdUnitID 的 EventTaskviewmodel 上。 ProductiveUnit 的所有字段都可以在视图中使用,或者只有“ProdUnitNo”,都没有关系。使用下面的 LINQ 请告知如何在 EventGantt_Read 操作中完成此操作:

public ActionResult EventGantt_Read([DataSourceRequest] DataSourceRequest request,int[] PUID)
{
    var data = (from e in db.Events.Where(t => PUID.Contains((int)t.ProductiveUnitID))
                join p in db.ProductiveUnits on e.ProductiveUnitID equals p.ProdUnitID
                select new { EventID = e.EventID,EventDescription = e.EventDescription,ProductiveUnitID = e.ProductiveUnitID,ActualStart = e.ActualStart,ActualEnd = e.ActualEnd,ParentEventID = e.ParentEventID,ActivityCodeID = e.ActivityCodeID,ProdUnitNo = p.ProdUnitNo }).ToList()
                .Select(eventList => new EventTaskviewmodel(eventList))
                .AsQueryable();
    return Json(data.ToDataSourceResult(request));
}

使用上述查询,我​​在以下行中收到错误

.Select(eventList => new EventTaskviewmodel(eventList))

eventList 上说无法从“

我的 EventTaskviewmodel:

namespace EMS.Models
{
    public class EventTaskviewmodel : IGanttTask
    {
        public EventTaskviewmodel()
        {
        }
        public EventTaskviewmodel(Event eventList)
        {
            ID = eventList.EventID;
            Title = eventList.EventDescription;
            ProductiveUnitID = eventList.ProductiveUnitID;
            Start = DateTime.SpecifyKind(eventList.ActualStart,DateTimeKind.Local); 
            End = DateTime.SpecifyKind(eventList.ActualEnd,DateTimeKind.Local);
            EventStatusID = eventList.EventStatusID;
            ParentID = eventList.ParentEventID;
            ActivityCodeID = eventList.ActivityCodeID;
        }

        public int ID { get; set; }
        public int? ParentID { get; set; }
        public string Title { get; set; }
        public int? ProductiveUnitID { get; set; }
        public string ActivityCodeID { get; set; }
       // public virtual ICollection<ActivityCode> ActivityCode { get; set; }
       // public ActivityCode Activity { get; set; }
        public string EventStatusID { get; set; }

        private DateTime start;
        public DateTime Start
        {
            get
            {
                return start;
            }
            set
            {
                start = value; //.ToUniversalTime();
            }
        }

        private DateTime end;
        public DateTime End
        {
            get
            {
                return end;
            }
            set
            {
                end = value;  //.ToUniversalTime();
            }
        }

        public bool Summary { get; set; }
        public bool Expanded { get; set; }
        public decimal PercentComplete { get; set; }
        public int OrderId { get; set; }

        public Event ToEntity()
        {
            return new Event
            {
                EventID = ID,ParentEventID = ParentID,EventDescription = Title,ProductiveUnitID = ProductiveUnitID,ActivityCodeID = ActivityCodeID,EventStatusID = EventStatusID,ActualStart = Start,ActualEnd = End,GanttSummary = Summary,GanttExpanded = Expanded,GanttPercentComplete = PercentComplete,GanttOrderID = OrderId
            };
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...