[英]Pass ID between 2 controllers ASP.NET Core 3.1 MVC
I'm new to programming and I need your help.我是编程新手,我需要你的帮助。 I'm trying to create a simple testing application (ASP.NET Core 3.1 MVC + EF).
我正在尝试创建一个简单的测试应用程序(ASP.NET Core 3.1 MVC + EF)。 I've created 2 models (Event, Enrollment).
我创建了 2 个模型(事件、注册)。 The goal is to open the enrollment create form page with the selected
EventId
by clicking on the "Enroll" button in the Events/Index
page.目标是通过单击“
Events/Index
页面中的“注册”按钮,使用选定的EventId
打开注册创建表单页面。 How should I pass the EventId
to the EnrollmentsController
?我应该如何将
EventId
传递给EnrollmentsController
? I tried to pass EventId
in the Events/Index
page using a form with hidden input, but I still don't know how to handle EventId
correctly in EnrollmentsController
.我尝试使用带有隐藏输入的表单在
Events/Index
页面中传递EventId
,但我仍然不知道如何在EnrollmentsController
正确处理EventId
。
Note: I don't wanna use SelectList
in Enrollment
's Create
method and view.注意:我不想在
Enrollment
的Create
方法和视图中使用SelectList
。
I will be grateful for any help.我将不胜感激任何帮助。 Thank you!
谢谢!
UPDATE: Thanks to @PanagiotisKanavos I finally get the EventId in the URL, but it still doesn't feed into the form input.更新:感谢@PanagiotisKanavos,我终于在 URL 中获得了 EventId,但它仍然没有输入到表单输入中。 There is "0".
有“0”。 What could be wrong?
可能有什么问题?
public class Event
{
public int EventId { get; set; }
public string Name { get; set; }
[DataType(DataType.Date)]
public DateTime Date { get; set; }
}
public class Enrollment
{
public int EnrollmentId { get; set; }
public int EventId { get; set; }
public Event Event { get; set; }
[Display(Name ="First name")]
public string FirstName { get; set; }
[Display(Name = "Last name")]
public string LastName { get; set; }
[Display(Name ="Enrollment date")]
[DataType(DataType.DateTime)]
public DateTime EnrollmentDate { get; set; }
}
Events/Index
view Events/Index
视图 @model IEnumerable<FormTest.Models.Event>
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Date)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
<td>
<a asp-action="Create" asp-controller="Enrollments" asp-route-id="@item.EventId" class="btn btn-primary">Enroll</a>
</td>
</tr>
}
</tbody>
</table>
Enrollments/Create
view Enrollments/Create
视图@model FormTest.Models.Enrollment
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>Enrollment</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="EventId" class="control-label"></label>
<input class="form-control" asp-for="EventId" name="EventId" />
</div>
<div class="form-group">
<label asp-for="FirstName" class="control-label"></label>
<input asp-for="FirstName" class="form-control" />
<span asp-validation-for="FirstName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="LastName" class="control-label"></label>
<input asp-for="LastName" class="form-control" />
<span asp-validation-for="LastName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EnrollmentDate" class="control-label"></label>
<input asp-for="EnrollmentDate" class="form-control" />
<span asp-validation-for="EnrollmentDate" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
EnrollmentsController
Create method EnrollmentsController
创建方法// GET: Enrollments/Create
public IActionResult Create(int eventId)
{
var newEnrollment = new Enrollment
{
EventId = eventId,
FirstName = "John",
LastName = "Doe",
EnrollmentDate = DateTime.UtcNow
};
return View(newEnrollment);
}
The easiest option would be to not use a form but a link to the Enrollments controller.最简单的选择是不使用表单,而是使用指向 Enrollments 控制器的链接。 After all, the goal is to open the
Enrollments
page, not submit the enrollment :毕竟,目标是打开
Enrollments
页面,而不是提交注册:
<td>
<a asp-controller="Enrollments"
asp-action="Create"
asp-route-id="@item.EventID">Enroll</a>
</td>
The link can be made to look like a button with the proper Bootstrap styles :可以使链接看起来像具有正确 Bootstrap 样式的按钮:
<a asp-controller="Enrollments"
asp-action="Create"
asp-route-id="@item.EventID"
class="btn btn-primary" role="button" >
Enroll
</a>
The Controller action needs to accept the id
as a parameter: Controller 动作需要接受
id
作为参数:
public IActionResult Create(int eventId)
{
var newEnrollment=new Enrollment{
EventId=eventId,
...
};
return View(newEnrollment);
}
The same routing tag helpers can be used in a form :可以在表单中使用相同的路由标签助手:
<td>
<form asp-controller="Enrollments"
asp-action="Create"
asp-route-id="@item.EventID" method="post">
<button type="submit" value="Enroll"></button>
</form>
</td>
Using a form like this is a bit weird though.不过,使用这样的表单有点奇怪。 This would require adding an
Index(id)
action that responds to POST
along with the Index(id)
that responds to GET
.这将需要添加一个
Index(id)
的行动,响应POST
与沿Index(id)
响应GET
。
The ASP.NET Core MVC Tutorial shows how to create an application that lists Todo items, allows editing, deleting and redirecting from the list to the edit pages. ASP.NET Core MVC 教程展示了如何创建一个应用程序来列出 Todo 项目,允许编辑、删除和从列表重定向到编辑页面。
The Tag Helpers in Forms page explains how tag helpers like asp-controller
, asp-action
and asp-route-*
can be used to specify controllers, actions and parameters in a form. 表单中的标签助手页面解释了如何使用标签助手(如
asp-controller
、 asp-action
和asp-route-*
来指定表单中的控制器、动作和参数。
If you want to use asp-route-{value} ,you need to make sure the value name is consistent to the parameter name in action.Fo example,you use如果你想使用asp-route-{value} ,你需要确保值名称与动作中的参数名称一致。例如,你使用
public IActionResult Create(int eventId)
So that you need to use所以你需要使用
asp-route-eventId="@item.EventId"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.