首页 > 解决方案 > 更新多对多关系控制器中的操作

问题描述

我正在使用 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");}
}

标签: c#asp.net-coreentity-framework-coreasp.net-core-mvc

解决方案


我在控制器中跟踪 Edit 方法,它总是在 Edit 帖子中发送 Null!

哪个值为空,user_idproject_id?您提供的表格似乎不包含他们的字段,他们是否有必要?

另外,html表单标签只支持GETandPUT方法,而edit是put方法,可以改成post。


推荐阅读