従業員のフル ネームのリストを含むビュー モデルからリストを返そうとしています。ただし、値は null として返され、その理由がわかりません。私はまた、この素敵なエラーに迎えられました:
オブジェクト参照がオブジェクト インスタンスに設定されていません。
このエラーは、AllEmployees() メソッドの foreach ループが問題であることを具体的に示しています
ここに私のエンティティがあります:
namespace ROIT.Entities
{
public class Employee : Entity<Employee>
{
public string FirstName { get; set; }
public string LastName { get; set; }
[NotMapped]
public string FullName {
get { return FirstName + " " + LastName; }
}
}
}
ここに私のビューモデルがあります:
namespace ROIT.Web.Models
{
public class ContractPageViewModel
{
public ICollection<Contract> Contracts { get; set; }
public Contract Contract { get; set; }
public ICollection<Roi> Rois { get; set; }
public ICollection<Employee> Employees { get; set; }
public ICollection<ContractResource> ContractResources { get; set; }
public ICollection<LaborCategory> LaborCategories{ get; set; }
public List<string> AllEmployeesList { get; set; }
public void AllEmployees()
{
AllEmployeesList = new List<string>();
foreach (Employee item in Employees)
{
AllEmployeesList.Add(item.FirstName);
}
}
}
}
そして、コントローラーの戻り値は次のとおりです。
public ActionResult testview()
{
var model = new ContractPageViewModel();
model.Contracts = db.Contracts.Include(c => c.Business).Include(c => c.Customer).ToList();
model.AllEmployees();
return View(model);
}
さらに明確にする必要がある場合はお知らせください。
前もって感謝します