c# - 更新多对多关系控制器中的操作
问题描述
我正在使用 Entity Framework Core Database-First,我有一个具有多对多关系的 UserProject 模型,我正在做控制器来更新它,但是当我在控制器中跟踪 Edit 方法时,我怎么能使用复合键它总是在编辑帖子中发送 Null!我做错了什么,我该如何解决?
控制器:
public async Task<IActionResult> Edit(int? user_id, int? project_id)
{
if (user_id == null && project_id == null )
{
return NotFound();
}
var userPoject = await _context.UserPojects.FindAsync(user_id , project_id);
if (userPoject == null)
{
return NotFound();
}
ViewData["ProjectId"] = new SelectList(_context.Projects, "ProjectId", "ProjectName", userPoject.ProjectId);
ViewData["UserId"] = new SelectList(_context.Users, "UserId", "UserName", userPoject.UserId);
return View(userPoject);
}
[HttpPut]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int? user_id , int? project_id, [Bind("UserId,ProjectId,UserRoles")] UserPoject userPoject)
{
if (project_id != userPoject.ProjectId && user_id != userPoject.UserId)
{
return NotFound();
}
_context.Entry(userPoject).State = EntityState.Modified;
if (ModelState.IsValid)
{
try
{
_context.Update(userPoject);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!UserPojectExists(userPoject.UserId))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
ViewData["ProjectId"] = new SelectList(_context.Projects, "ProjectId", "ProjectName", userPoject.ProjectId);
ViewData["UserId"] = new SelectList(_context.Users, "UserId", "UserName", userPoject.UserId);
return View(userPoject);
}
查看索引:
<td>
@Html.ActionLink("Edit", "Edit", new { user_id = item.UserId , project_id =item.ProjectId }) |
@Html.ActionLink("Details", "Details", new { id = item.UserId }) |
@Html.ActionLink("Delete", "Delete", new { user_id = item.UserId, project_id = item.ProjectId })
</td>
编辑视图:
@model SystemAdminDBFApp.Models.UserPoject
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>UserPoject</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input asp-for="UserId" />
<input asp-for="ProjectId" />
<div class="form-group">
<label asp-for="UserRoles" class="control-label"></label>
<input asp-for="UserRoles" class="form-control" />
<span asp-validation-for="UserRoles" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
解决方案
我在控制器中跟踪 Edit 方法,它总是在 Edit 帖子中发送 Null!
哪个值为空,user_id
或project_id
?您提供的表格似乎不包含他们的字段,他们是否有必要?
另外,html表单标签只支持GET
andPUT
方法,而edit是put方法,可以改成post。
推荐阅读
- drupal - 对 Drupal 的 Axios 身份验证请求 - invalid_grant
- java - 升级到版本后的 EasyImage 问题。3.0.1
- php - 循环 JSON 图像 - 关于高级自定义字段
- javascript - 如何在Vue中根据选择更改输入的占位符
- accounting - 精确在线 - 是否可以明确匹配 GeneralJournalEntry 和 BankEntry 单个条目行?
- azure - 使用 kubectl 执行 CURL
- javascript - 使用道具为div添加背景
- sql - USE dbName VS dbName.TableName 的区别?
- wordpress - 在 Google Cloud 和 Cloud SQL 之间设置 SSL
- visual-studio-code - 如何自定义差异编辑器更改的文本颜色