I have the following 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; }
}
In my controller I have this definition of the model to be sent to the view:
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();
My problem is the following lines in the definition: //machineryList = _db.Machinery.ToList(), //cleaningList = _db.Cleaning.ToList(),
Everything works as expected, but as soon as I enable those 2 lines it breaks with null errors. VS can compile, but it breaks runtime.
I think I see the problem, but I don't know how to solve it. I want ALL fields in the ViewModel EXCEPT the 2 mentioned lines to be a list, and then the 2 lines to be separate lists independent of the majority. I have tried all combinations of moving those lines around, but then VS complains.
An example from another controller is this:
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(),
};
This works perfectly, but the former model definition as more complex, so basically, I need something similar to the latter example separating the 2 lists from the other properties in the ViewModel. Maybe this is VERY simple - however I'm struggling with it...
Anyone see the solution to this?
CodePudding user response:
I don't think that you model is a list with so many includes.
I guess you really need this ViewModel
public class ViewModel
{
public List<DayTaskListViewModel> DayTaskListViewModelList {get; set;}
public List<Machinery> MachineryList {get; set;}
public List<Cleaning> CleaningList {get; set;}
}
code
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(),
CodePudding user response:
Tried your latest suggestion, but then VS complains:
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,
}).ToList();
model.machineryList = _db.Machinery.ToList();
model.cleaningList = _db.Cleaning.ToList();
"machineryList" and "cleaningList" after "model." above i marked in red in VS, and the error is:
List<DayTaskListViewModel> does not contain a definition for machineryList and no accessable extension method...
CodePudding user response:
Here is a snip of the view:
Top:
@model List<Day.Models.ViewModels.DayTaskListViewModel>
@using Day.Extensions
Looping through all properties:
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...
}
}
Works fine.
Then I need to include select fields using the machineryList from the ViewModel:
<select name="machinery" asp-items="@Model.machineryList.ToSelectListItem((int)defaultMachinery)"></select>