【发布时间】:2021-10-04 21:37:24
【问题描述】:
我是编程新手,需要您的帮助。我正在尝试创建一个简单的测试应用程序(ASP.NET Core 3.1 MVC + EF)。我创建了 2 个模型(事件、注册)。目标是通过单击Events/Index 页面中的“注册”按钮打开带有选定EventId 的注册创建表单页面。我应该如何将EventId 传递给EnrollmentsController?我尝试使用带有隐藏输入的表单在Events/Index 页面中传递EventId,但我仍然不知道如何在EnrollmentsController 中正确处理EventId。
注意:我不想在Enrollment 的Create 方法和视图中使用SelectList。
如果有任何帮助,我将不胜感激。谢谢!
更新:感谢@PanagiotisKanavos,我终于在 URL 中获得了 EventId,但它仍然没有输入到表单输入中。有“0”。有什么问题?
型号
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查看
@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查看
@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创建方法
// GET: Enrollments/Create
public IActionResult Create(int eventId)
{
var newEnrollment = new Enrollment
{
EventId = eventId,
FirstName = "John",
LastName = "Doe",
EnrollmentDate = DateTime.UtcNow
};
return View(newEnrollment);
}
【问题讨论】:
-
控制器代码在哪里?您是否有特定问题,或者您是否在询问控制器的一般工作方式?你试过ASP.NET Core MVC tutorial吗?
Controllers and Views部分准确显示您的要求。<a asp-action="Edit" asp-route-id="@item.ID">Edit</a>生成一个以 ID 作为参数调用Edit的链接。您可以使用样式使链接看起来像一个按钮。你可以在asp-action中传递不同的动作名称 -
您的意思是要将隐藏输入的值传递给操作?
-
如果你真的需要
POST,你可以在链接按钮的onclick处理程序中使用fetch。如果你真的想要一个表格,你可以使用Form tag helpers。例如,asp-route-id="1234"会将1234传递给id参数 -
您的隐藏输入缺少
name属性:<input type="hidden" value="@Html.DisplayFor(modelItem => item.EventId)" />。浏览器会忽略没有名字的输入 -
嗨,@CyberAnt,尝试检查我更新的答案,您在
asp-route-{value}中的值名称与操作Create的参数名称不一致。
标签: c# asp.net-core asp.net-core-mvc asp.net-core-routing