如何从一个表中获取一个ID并将其保存到ASP.NET Core存储库模式中的另一个表中

问题描述

这是计划考试服务:

ModuleNotFoundError: No module named 'pyFlink'

这是计划考试控制者:

public int AddSchedule(ScheduleExamviewmodel schedule)
{
        var newSchedule = new ScheduleExam()
        {
            ExamDate = schedule.ExamDate,SubjectId = schedule.SubjectId,StudentId = schedule.StudentId,Status = schedule.Status
        };

        _context.ScheduleExams.Add(newSchedule);
        _context.SaveChanges();

        return newSchedule.Id;
}

我想获取学生的ID并通过点击“计划新考试”将其保存到计划考试表中

这是Student表的// GET: ScheduleExamController/Create public IActionResult Create() { var model = new ScheduleExamviewmodel(); ViewBag.Subject = new SelectList(_isubject.GetSubject(),"Id","Name"); ViewBag.Student = new SelectList(_istudent.GetStudent(),"Name"); return View(model); } // POST: ScheduleExamController/Create [HttpPost] [ValidateAntiForgeryToken] public IActionResult Create(ScheduleExamviewmodel schedule) { if (ModelState.IsValid) { int id = _ischeduleExam.AddSchedule(schedule); if (id > 0) { return RedirectToAction(nameof(Create)); } } ViewBag.Subject = new SelectList(_isubject.GetSubject(),"Name"); return View(schedule); }

index.cshtml

这是日程安排考试的create.csthml页面

<table class="table table-hover table-bordered table-condensed">
            <thead class="table-color text-white">
                <tr>
                    <th>
                        @Html.displayNameFor(model => model.Id)
                    </th>
                    <th>
                        @Html.displayNameFor(model => model.Name)
                    </th>
                    <th>
                        @Html.displayNameFor(model => model.LastName)
                    </th>
                    <th>
                        @Html.displayNameFor(model => model.Email)
                    </th>
                    <th>Schedule New Exam</th>
                    <th>Properties</th>

                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                <tr>
                    <td>
                        @Html.displayFor(modelItem => item.Id)
                    </td>
                    <td>
                        @Html.displayFor(modelItem => item.Name)
                    </td>
                    <td>
                        @Html.displayFor(modelItem => item.LastName)
                    </td>
                    <td>
                        @Html.displayFor(modelItem => item.Email)
                    </td>
                    <td>
                        <a asp-action="Create" asp-controller="ScheduleExam",asp-route-id="@item.Id">Schedule New Exam</a>
                    </td>
                    <td>
                        @Html.ActionLink(" ","Edit",new { id = item.Id },new { @class = "fa fa-edit",title = "Edit" }) &nbsp;|&nbsp;
                        @Html.ActionLink(" ","Details",new { @class = "fa fa-info-circle",title = "More details" }) &nbsp;|&nbsp;
                        @Html.ActionLink(" ","Delete",new { @class = "fa fa-trash",title = "Delete" })
                    </td>
                </tr>
                }
            </tbody>
        </table>

但是当我单击“学生”表的index.cshtml中的“安排新考试”时,出现此错误

RuntimeBinderException:无法对空引用执行运行时绑定 CallSite.Target(Closure,CallSite,object)

CallSite.Target(Closure,CallSite,object) System.Dynamic.UpdateDelegates.UpdateAndExecute1 (CallSite网站,T0 arg0)
Create.cshtml中的AspNetCore.Views_ScheduleExam_Create.b__22_0() +

 <form asp-action="Create">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>

        <div class="form-group">
            <label asp-for="ExamDate" class="control-label"></label>
            <input asp-for="ExamDate" class="form-control" />
            <span asp-validation-for="ExamDate" class="text-danger"></span>
        </div>
        <div class="form-group">
        <label asp-for="StudentId" class="control-label"></label>
        <input asp-for="StudentId" class="form-control" value="@ViewBag.model.Id" />
        <span asp-validation-for="StudentId" class="text-danger"></span>
    </div>
        <div class="input-group">
            <div class="input-group-prepend">
                <span class="input-group-text"><strong>Subject:</strong></span>
            </div><br />
            <select asp-for="SubjectId" class="form-control input-hover" asp-items="ViewBag.Subject">
                <option value="">Please choose a subject...</option>
            </select>
            <span asp-validation-for="SubjectId " class="text-danger"></span>
        </div><br />
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-primary" />
        </div>
    </form>

请通过细节解决它,我正在使用带有存储库模式的ASP.NET Core 3.1 MVC。

谢谢

解决方法

首先,您应该在Create-Action中获得一个ID并为学生设置:

public IActionResult Create(int Id)
{
    var model = new ScheduleExamViewModel();
    
    ViewBag.Subject = new SelectList(_isubject.GetSubject(),"Id","Name");
    ViewBag.Student = new SelectList(_istudent.GetStudent(),"Name",Id);

    return View(model);
}

第二,我在您的视图中找不到学生的下拉列表。我认为您应该在ViewModel的控制器中设置ID。

var model = new ScheduleExamViewModel();
mode.Id = Id;

第三,您将模型传递给视图,您可以从模型中获取信息。

@model ScheduleExamViewModel
<input asp-for="StudentId" class="form-control" value="@Model.Id" />