【发布时间】:2021-08-09 19:07:27
【问题描述】:
我检查了有关此问题的大部分问题,但没有一个解决方案对我有用。
我有这个 IActionResult 可以获取数据并且它可以工作
[HttpGet]
[AllowAnonymous]
public IActionResult AddUser(SearchViewModel model)
{
model.Location = new List<Location>();
foreach (Location loc in Enum.GetValues(typeof(Location)))
{
model.Location.Add(loc);
}
model.FoundRoles = new List<Roles>();
if (roleManager.Roles != null)
{
foreach (var role in roleManager.Roles)
{
Roles roles = new Roles
{
RoleId = role.Id,
RoleName = role.Name
};
model.FoundRoles.Add(roles);
}
}
return View(model);
}
现在在 HttpPost 中,当我单击提交按钮时,数据丢失,它返回 Null,因此我将 get 请求中的相同代码复制到帖子中,但它不起作用,它被忽略了
[HttpPost]
[AllowAnonymous]
public IActionResult AddUser(SearchViewModel profile, string id, string button, List<User> users)
{
if(ModelState.IsValid)
{
//Some code here
}
profile.Location = new List<Location>();
foreach (Location loc in Enum.GetValues(typeof(Location)))
{
profile.Location.Add(loc);
}
profile.FoundRoles = new List<Roles>();
if (roleManager.Roles != null)
{
foreach (var role in roleManager.Roles)
{
Roles roles = new Roles
{
RoleId = role.Id,
RoleName = role.Name
};
profile.FoundRoles.Add(roles);
}
}
return View(profile);
}
在我看来,我正在使用 for 循环,就像其他问题中建议的其他人一样。我也尝试使用隐藏字段,但没有任何效果
我该如何解决这个问题
【问题讨论】:
-
当您说“数据丢失它返回 Null”时,您所说的数据是什么意思
-
嗨@r2000_na,你能分享你的观点吗?也请分享您的模型设计。
标签: c# asp.net-core model-view-controller asp.net-core-mvc