我有以下ViewModels;
public class InformationSystemsViewModel
{
public List<RoleViewModel> Roles { get; set; }
}
public class RoleViewModel
{
public string Name { get; set; }
public Guid HireRequestId { get; set; }
public string RoleId { get; set; }
public List<CompanyViewModel> Companies { get; set; }
}
public class CompanyViewModel
{
public int CompanyId { get; set; }
public bool IsSelected { get; set; }
public string Name { get; set; }
}
以下代码在我的控制器中;
public ActionResult InformationSystems(Guid Id)
{
var nhr = oimRepo.GetHireRequest(Id);
var vm = AutoMapper.Mapper.Map<HireRequest, InformationSystemsViewModel>(nhr);
IEnumerable<Company> companies = ListCompanies();
IEnumerable<Role> roles = ListRoles();
List<RoleViewModel> listOfRoles = roles.Select(x => new RoleViewModel()
{
Name = x.Role,
HireRequestId = Id,
RoleId = x.RoleId,
Companies = companies.Select(y => new CompanyViewModel()
{
CompanyId = y.CompanyId,
Name = y.Company,
IsSelected = (vm.Roles == null ? false : vm.Roles.Any(a => a.HireRequestId == Id && a.RoleId == x.RoleId))
}).ToList()
}).ToList();
vm.Roles = listOfRoles;
return View(vm);
}
[HttpPost]
public ActionResult InformationSystems(InformationSystemsViewModel Model)
{
if (ModelState.IsValid)
{
HireRequest nhr = oimRepo.GetHireRequest(Model.Id);
AutoMapper.Mapper.Map<InformationSystemsViewModel, HireRequest>(Model, nhr);
oimRepo.Save(CurrentUser.Id);
var roles = Model.Roles;
var rList = new List<RoleViewModel>();
var cList = new List<CompanyViewModel>();
foreach(var r in roles)
{
var companies = r.Companies;
if(companies != null)
{
foreach(var c in companies)
{
var cvm = new CompanyViewModel()
{
CompanyId = c.CompanyId,
IsSelected = c.IsSelected,
Name = c.Name
};
cList.Add(cvm);
}
var rvm = new RoleViewModel()
{
Companies = cList,
HireRequestId = r.HireRequestId,
Name = r.Name,
RoleId = r.RoleId
};
rList.Add(rvm);
}
}
var gpAccess = AutoMapper.Mapper.Map<IEnumerable<RoleViewModel>, List<HireRequest_Access>>(rList);
oimRepo.CreateGPAccess(gpAccess);
oimRepo.Save(CurrentUser.Id);
}
return RedirectToAction("Review", new { id = Model.Id });
}
return View(Model);
}
最后我的Razor标记;
<table id="Roles" data-height="600" data-toggle="table" data-show-columns="false" data-striped="true" data-fixed-columns="false" data-fixed-number="1">
<thead>
<tr>
<th>Role</th>
@foreach (var company in Model.Roles.FirstOrDefault().Companies)
{
<th data-id="@company.CompanyId" data-field="@company.Name">@company.Name</th>
}
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Roles.Count; i++)
{
<tr>
<td data-id="@Model.Roles[i].RoleId" class="gp-role-name">
@Model.Roles[i].RoleId
@Html.HiddenFor(m => m.Roles[i].RoleId)
@Html.HiddenFor(m => m.Roles[i].Name)
@Html.HiddenFor(m => m.Roles[i].HireRequestId)
</td>
@for (int j = 0; j < Model.Roles[i].Companies.Count; j++)
{
<td class="gp-company-checkbox">
@Html.HiddenFor(m => m.Roles[i].Companies[j].CompanyId)
@Html.HiddenFor(m => m.Roles[i].Companies[j].Name)
@Html.CheckBoxFor(m => m.Roles[i].Companies[j].IsSelected)
</td>
}
</tr>
}
</tbody>
</table>
发布回发后,角色视图模型中的公司集合始终为空。我确定这是明显的,但我看不出我做错了什么。
回发行动中的代码非常冗长,因为我试图逐步完成它并弄清楚出了什么问题。