简体   繁体   English

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

[英]How to get an Id from one table and save it to another table in ASP.NET Core, repository pattern

This is Schedule Exam service:这是安排考试服务:

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;
}

This is Schedule exam controller :这是安排考试控制器:

// GET: ScheduleExamController/Create
public IActionResult Create()
{
    var model = new ScheduleExamViewModel();

    ViewBag.Subject = new SelectList(_isubject.GetSubject(), "Id", "Name");
    ViewBag.Student = new SelectList(_istudent.GetStudent(), "Id", "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(), "Id", "Name");
    ViewBag.Student = new SelectList(_istudent.GetStudent(), "Id", "Name");

    return View(schedule);
}

I want to get the Id of student and save it to table of schedule exam by clicking on the "Schedule New Exam"我想通过单击“安排新考试”来获取学生的 ID 并将其保存到安排考试表中

This is the index.cshtml of the Student table:这是 Student 表的index.cshtml

<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 { id = item.Id }, new { @class = "fa fa-info-circle", title = "More details" }) &nbsp;|&nbsp;
                        @Html.ActionLink(" ", "Delete", new { id = item.Id }, new { @class = "fa fa-trash", title = "Delete" })
                    </td>
                </tr>
                }
            </tbody>
        </table>

and this is the create.csthml page of schedule exam:这是日程安排考试的 create.csthml 页面:

 <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>

but when I click on the Schedule New exam in index.cshtml of Student table I get this error:但是当我单击 Student 表的 index.cshtml 中的 Schedule New 考试时,我收到此错误:

RuntimeBinderException: Cannot perform runtime binding on a null reference CallSite.Target(Closure , CallSite , object ) RuntimeBinderException:无法对空引用执行运行时绑定 CallSite.Target(Closure , CallSite , object )

CallSite.Target(Closure , CallSite , object ) System.Dynamic.UpdateDelegates.UpdateAndExecute1<T0, TRet>(CallSite site, T0 arg0) CallSite.Target(Closure, CallSite, object) System.Dynamic.UpdateDelegates.UpdateAndExecute1<T0, TRet>(CallSite site, T0 arg0)
AspNetCore.Views_ScheduleExam_Create.b__22_0() in Create.cshtml + Create.cshtml 中的 AspNetCore.Views_ScheduleExam_Create.b__22_0() +

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

Please solve it by details and I'm using ASP.NET Core 3.1 MVC using a repository pattern.请详细解决它,我正在使用使用存储库模式的 ASP.NET Core 3.1 MVC。

Thank you谢谢

first of all, you should get an Id inside Create-Action and set for students :首先,您应该在 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(), "Id", "Name" , Id);

    return View(model);
}

Second, I can't find a drop-down list for Students in your view.其次,我在您的视图中找不到学生的下拉列表。 I think you should set Id in the controller for ViewModel.我认为您应该在 ViewModel 的控制器中设置 Id。

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

Third, you passed a model to the View and you can get information from the Model.第三,您将模型传递给视图,您可以从模型中获取信息。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net mvc 5如何从URL获取ID并将其保存在另一个表中 - asp.net mvc 5 How to get id from url and save it in another table 如何在 ASP.NET MVC 中将一个表的 ID 保存到另一个表? - How to save an ID from a table to another table in ASP.NET MVC? 如何获取身份 id 类型 guid 并分配给另一个表中的外键 id。 适用于Asp.net内核 - How to get identity id type guid and assign to foreign key id in another table. Apply in Asp.net Core 我想使用asp.net mvc 5将一个表中的ID保存到另一个表中 - I want to save an ID from a table to another table using asp.net mvc 5 使用 ASP.NET MVC Core 中的工作单元模式,使用从另一个表中提取的 Id 列表从表中检索数据 - Retrieve data from a table using a list of Ids extracted from another table using the Unit of Work Pattern in ASP.NET MVC Core 从一个表中选择一个值以将该行的ID插入asp.net中的另一个表 - Selecting a value from one table to insert the id of the row to another one in asp.net 如何在一次单击中获取两个表ID以保存在asp .net mvc 4中的第三个表中 - how to get two table id in one click to save in third table in asp .net mvc 4 Asp.net 核心 -&gt; 如何使用 pKey(invoice_id, row_id) 从表中删除行 - Asp.net core -> How to Delete Row from table with pKey(invoice_id, row_id) 获取最近的ID并将其插入ASP.NET中的另一个表 - Get the recent ID and insert it to another table in ASP.NET 获取上一次生成的ID并将其添加到另一个表ASP.NET - Get last generated ID and add it to another table ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM