我有一个带有两个动作的控制器。一个操作只是更新数据库的帖子。另一种是视图模型。但是,在我更新数据库的操作中,我将其返回到具有视图模型的原始视图。
public ActionResult ManageMxieUsers()
{
var model = from el in aphdb.view_EmployeeList
join p in aphdb.MxieCallRevPermissions on el.Id equals p.AspNetUserUserID into gj
from sub in gj.DefaultIfEmpty()
select new vm_ManageMxieUsers { Id = el.Id, UserName = el.UserName, PermissionLevel = sub.PermissionLevel, MxieCallRevPermCustomList = aphdb.MxieCallRevPermCustomList.Where(w => w.PermissionID == sub.PermissionID ) };
ViewBag.EmployeeList = aphdb.view_EmployeeList.OrderBy(o => o.UserName);
return View(model);
}
这部分完美无缺。
这是我的帖子方法:
[HttpPost]
public ActionResult ManageMxieUsers(string userID, Int16 permissionLevel, List<string> customUserList)
{
var UserToEdit = aphdb.MxieCallRevPermissions.Where(w => w.AspNetUserUserID == userID).FirstOrDefault();
if (UserToEdit == null)
{
MxieCallRevPermissions addPerm = new MxieCallRevPermissions();
addPerm.AspNetUserUserID = userID;
addPerm.PermissionLevel = permissionLevel;
aphdb.MxieCallRevPermissions.Add(addPerm);
aphdb.SaveChanges();
if (permissionLevel == 3)
{
foreach (var id in customUserList)
{
MxieCallRevPermCustomList list = new MxieCallRevPermCustomList();
list.PermissionID = addPerm.PermissionID;
list.AspNetUserID = id;
aphdb.MxieCallRevPermCustomList.Add(list);
}
}
aphdb.SaveChanges();
@ViewBag.Success = true;
}
else
{
UserToEdit.PermissionLevel = permissionLevel;
aphdb.SaveChanges();
if (permissionLevel == 3)
{
// Remove old custom list
var customList = aphdb.MxieCallRevPermCustomList.Where(w => w.PermissionID == UserToEdit.PermissionID).ToList();
aphdb.MxieCallRevPermCustomList.RemoveRange(customList);
aphdb.SaveChanges();
foreach (var id in customUserList)
{
MxieCallRevPermCustomList list = new MxieCallRevPermCustomList();
list.PermissionID = UserToEdit.PermissionID;
list.AspNetUserID = id;
aphdb.MxieCallRevPermCustomList.Add(list);
}
aphdb.SaveChanges();
}
else
{
// Remove old custom list
var customList = aphdb.MxieCallRevPermCustomList.Where(w => w.PermissionID == UserToEdit.PermissionID).ToList();
aphdb.MxieCallRevPermCustomList.RemoveRange(customList);
aphdb.SaveChanges();
}
aphdb.SaveChanges();
@ViewBag.Success = true;
}
//aphdb.SaveChangesAsync(); // Testing ot make sure SaveChanges fired before this code.
var model = from el in aphdb.view_EmployeeList
join p in aphdb.MxieCallRevPermissions on el.Id equals p.AspNetUserUserID into gj
from sub in gj.DefaultIfEmpty()
select new vm_ManageMxieUsers { Id = el.Id, UserName = el.UserName, PermissionLevel = sub.PermissionLevel, MxieCallRevPermCustomList = aphdb.MxieCallRevPermCustomList.Where(w => w.PermissionID == sub.PermissionID) };
ViewBag.EmployeeList = aphdb.view_EmployeeList.OrderBy(o => o.UserName);
//return RedirectToAction("ManageMxieUsers"); // WORKS
return View("ManageMxieUsers", model); // ERROR NULL on AspNetUser
}
现在我只是return View("ManageMxieUsers", model);
错误回来说AspNetUsers为空。但是,当我重新加载此页面时,它工作得很好。当我return RedirectToAction("ManageMxieUsers");
时,我没有收到任何错误,但当然我丢失了ViewBag
。
我唯一的猜测是aphdb.SaveChanges()
没有立即反映出来?即便如此,我也可以对数据库执行SQL查询,并确实已经添加/更新了数据。正如重新加载当前页面所引用的那样。
所以我不知道为什么在这个post方法中我的视图模型部分会出现空条目,但是当我重新加载它时,它们会正常填充。
我可以将其保留为RedirectToAction,但我想更好地理解为什么这种行为能够使其发挥作用。
这是打破
的视图部分<tbody>
@{ int? permissionLevel; }
@foreach (var user in Model)
{
permissionLevel = user.PermissionLevel ?? 4;
<tr>
<td>@user.Id</td>
<td>
<a href="#" class="mr5 selectUser" data-toggle="modal" data-userid="@user.Id" data-username="@user.UserName" data-permissionlevel="@user.PermissionLevel" data-target=".bs-example-modal-panel">@user.UserName</a>
</td>
<td>@permissionLevel</td>
<td>
@foreach (var customUser in user.MxieCallRevPermCustomList)
{
@("[" + customUser.AspNetUsers.FirstName + " " + customUser.AspNetUsers.Lastname + "] ")
}
</td>
</tr>
}
</tbody>
在我的customUser.AspNetUsers = null
方法中使用return View("ManageMxieUsers", model);
时,{p> [HttpPost]
。