【问题标题】:ViewModel with mixed fields and lists具有混合字段和列表的 ViewModel
【发布时间】:2021-12-12 00:11:30
【问题描述】:

我有以下 ViewModel:

public class DayTaskListViewModel
{
    public int Id { get; set; }
    public string DateFormatted { get; set; }
    public bool HasRegistrations { get; set; }
    public bool HasStartedRegistrations { get; set; }
    public string ItemName { get; set; }
    public string WorkTypeName { get; set; }
    public string Description { get; set; }
    public string LocationName { get; set; }

    public bool IsActive { get; set; }
    public string UserName { get; set; }
    public string StateStatus { get; set; }

    public DateTime TodaysDate { get; set; }
    public int WeekNumber { get; set; }
    public int YearNumber { get; set; }
    public string Msg { get; set; }
    public string SignInUserName { get; set; }

    public string UnitCode { get; set; }
    public IEnumerable<Machinery> machineryList { get; set; }
    public IEnumerable<Cleaning> cleaningList { get; set; }
}

在我的控制器中,我有这个要发送到视图的模型定义:

        var model = (from a in _db.WorkTask.Where(y => y.TaskDate.Date == querytodaysDate.Date && y.IsActive == true && (y.IsPrivateUserName == null || y.IsPrivateUserName == currUserName))
                      join b in _db.WorkTaskLog.Where(x => (x.UserName == currUserName || x.UserName == null) && x.IsActive == true && x.StateStatusId < 4) on a.Id equals b.WorkTaskId into joinedT
                      from c in joinedT.DefaultIfEmpty()
                      select new DayTaskListViewModel
                      {
                          Id = a.Id,
                          DateFormatted = a.DateFormatted,
                          HasRegistrations = a.HasRegistrations,
                          HasStartedRegistrations = a.HasStartedRegistrations,
                          ItemName = a.ItemName,
                          WorkTypeName = a.WorkTypeName,
                          Description = a.Description,
                          IsActive = c.IsActive ? c.IsActive : false,
                          UserName = c.UserName ?? String.Empty,
                          StateStatus = c.StateStatus ?? "Klar",
                          WeekNumber = (int)currWeekNo,
                          YearNumber = (int)currYearNo,
                          Msg = "",
                          TodaysDate = (DateTime)todaysDate,
                          SignInUserName = currUserName,
                          LocationName = a.LocationName,
                          UnitCode = a.UnitCode,
                          //machineryList = _db.Machinery.ToList(),
                          //cleaningList = _db.Cleaning.ToList(),
                      }).ToList();

我的问题是定义中的以下几行: //machineryList = _db.Machinery.ToList(), //cleaningList = _db.Cleaning.ToList(),

一切都按预期工作,但是一旦我启用这 2 行,它就会因空错误而中断。 VS 可以编译,但是会破坏运行时。

我想我看到了问题,但我不知道如何解决它。 我希望 ViewModel 中的所有字段除了提到的 2 行是一个列表,然后 2 行是独立于大多数的单独列表。 我已经尝试了移动这些线条的所有组合,但随后 VS 抱怨。

另一个控制器的示例如下:

            DriveListViewModel model = new DriveListViewModel()
            {
                Drive = await _db.Drive
                    .Where(m => m.StatusId == 5 || m.StatusId == 1010 || m.StatusId == 1012)
                    .Where(m => m.LoadingComplete == null)
                    .Where(m => !m.UnitCode.Contains(excludeString))
                    .Include(s => s.DriveStatus)
                    .Include(d => d.Location)
                    .Include(f => f.Item)
                    .GroupBy(m => m.RegistrationNumber)
                    .Select(m => m.FirstOrDefault())
                    .OrderBy(m => m.DriverToLoad)
                    .ToListAsync(),
                machineryList = await _db.Machinery.ToListAsync(),
                cleaningList = await _db.Cleaning.ToListAsync(),
            };

这很好用,但前一个模型定义更复杂,所以基本上,我需要类似于后一个示例的东西,将 2 个列表与 ViewModel 中的其他属性分开。 也许这很简单 - 但是我正在努力解决它......

有人看到这个问题的解决方案吗?

【问题讨论】:

  • 那么...为什么不在查询之后设置这两个属性呢?执行Select,然后执行var machineryList = await _db.Machinery.ToListAsync(); var cleaningList = await _db.Cleaning.ToListAsync(); model.ForEach(m =&gt; { m.machineryList = machineryList; m.cleaningList = cleaningList; });
  • 你好异端。我真的没有接受你的建议!非常感谢,它现在有效,我可以继续。非常感谢。你和 Serge ROCKS!!!

标签: c# list linq viewmodel


【解决方案1】:

我不认为你的模型是一个包含这么多包含的列表。

我猜你真的需要这个 ViewModel

public class ViewModel
{
  public  List<DayTaskListViewModel> DayTaskListViewModelList {get; set;}
  public List<Machinery> MachineryList {get; set;}
  public List<Cleaning>   CleaningList {get; set;}
}

代码

 var dayTaskListViewModel= (from ....
 .....
  select new DayTaskListViewModel
  {
    Id = a.Id,
   .......
                   
  }).ToList();  // or ToListAsync too?

var model = new ViewModel
{
   DayTaskListViewModel = dayTaskListViewModel,
   MachineryList = _db.Machinery.ToList(),
   CleaningList = _db.Cleaning.ToList()
}

   // or if you can or prefer , use async
 MachineryList =  await _db.Machinery.ToListAsync(),
  CleaningList = await _db.Cleaning.ToListAsync(),

【讨论】:

  • 嗨,谢尔盖。啊,所以我可以将一个 ViewModel 包装到另一个 ViewModel 中,从而像您显示的那样分隔列表?
  • 是的,它更清楚,或者您可以使用它。请参阅我的更新答案。
  • @RenéWester 对不起,这是一个错误。如果您发布使用此模型的视图,那就太好了。您不需要发布所有属性,每种属性只需发布几个
  • 我使用 Heritic Monkeys 解决方案成功了,但您的绝对值得一试。你和异端摇滚!非常感谢你们俩。非常感谢您的帮助。
【解决方案2】:

这是视图的一个片段:

顶部:

@model List<Day.Models.ViewModels.DayTaskListViewModel>
@using Day.Extensions

循环遍历所有属性:

if (Model.Count > 0)
{
    foreach (var item in Model)
    {
<input name="description" class="form-input" id="description" type="text" value="@item.Description">
    ..Several input fields...
    }
}

工作正常。

然后我需要使用 ViewModel 中的 MachineryList 包含选择字段:

<select name="machinery" asp-items="@Model.machineryList.ToSelectListItem((int)defaultMachinery)"></select>

【讨论】:

    猜你喜欢
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    相关资源
    最近更新 更多