Entity framework core 如何从视图中获取数据并将其发送到控制器操作?ASP.NET核心MVC
我有一个视图,其中显示了Entity framework core 如何从视图中获取数据并将其发送到控制器操作?ASP.NET核心MVC,entity-framework-core,asp.net-core-mvc,Entity Framework Core,Asp.net Core Mvc,我有一个视图,其中显示了请求表中的所有记录。每个记录都有一个保存和删除按钮,因此用户可以向ReqHistory表添加或删除请求 如何从请求视图中获取每条记录的数据(ID、标题、说明等),并将其传递到ReqHistoryController中的添加/删除操作 当我提交表单时,它不会从视图中获取记录的数据 有人能帮我吗 @model IEnumerable<App.Models.Request> @foreach (var item in Model) { <form a
请求
表中的所有记录。每个记录都有一个保存和删除按钮,因此用户可以向ReqHistory
表添加或删除请求
如何从请求视图中获取每条记录的数据(ID、标题、说明等),并将其传递到ReqHistoryController
中的添加/删除操作
当我提交表单时,它不会从视图中获取记录的数据
有人能帮我吗
@model IEnumerable<App.Models.Request>
@foreach (var item in Model)
{
<form asp-action="AddReqToHistory" asp-controller="ReqHistory">
<div class="card border-dark mb-3" style="max-width: 30rem;">
<div class="card-header">
@item.Title
</div>
<div class="card-body text-dark">
<img class="imageThumbnail" src="~/images/@item.ImagePath" asp-append-version="true" alt="image" />
<p class="card-text">
<br />
@item.Description
</p>
<a asp-action="Details" asp-route-id="@item.ID" >
Details
</a>
<input asp-action="AddReqToHistory" asp-controller="ReqHistory" type="submit" value="Save" />
<input asp-action="EliminateReqFromHistory" asp-controller="ReqHistory" type="submit" value="Eliminate" />
</div>
</div>
</form>
}
ReqHistory
model class:
public class ReqHistory
{
[Key]
public string UserId { get; set; }
[Key]
public int RequestId { get; set; }
public ICollection<Request> Requests { get; set; } //1-M relationship
public string Title { get; set; }
public string Description { get; set; }
public string ImagePath { get; set; }
//one-to-one relationship
public int CategoryID { get; set; }
public Category Category { get; set; }
public int PersonID { get; set; } //FK
public Person Person { get; set; } //nav property
}
您可以通过url方法通过asp路由传递id。这只是基本的HTML 101。您可能需要一个或多个HTML表单,其中包含该数据的隐藏输入,或者需要使用AJAX发出请求,并通过从页面中选择数据动态构建帖子正文。在这两种情况下,关键是您必须以可以发送数据的方式提供数据。页面上的随机数据不仅仅是自动发送的。那没有意义。我知道这是一个愚蠢的问题,但我是一个初学者,我还在学习。谢谢你的回答!祝您有个美好的一天!理解。我的观点是,您需要学习HTML和JavaScript之类的基础知识。当你不知道基本的web技术是如何工作的时候,你不能直接跳入web应用程序。
public class ReqHistory
{
[Key]
public string UserId { get; set; }
[Key]
public int RequestId { get; set; }
public ICollection<Request> Requests { get; set; } //1-M relationship
public string Title { get; set; }
public string Description { get; set; }
public string ImagePath { get; set; }
//one-to-one relationship
public int CategoryID { get; set; }
public Category Category { get; set; }
public int PersonID { get; set; } //FK
public Person Person { get; set; } //nav property
}
@model IEnumerable<App.Models.ReqHistory>
<table class="table table-condensed table-bordered">
<tr>
<th>
@Html.DisplayNameFor(model => model.RequestId)
</th>
<th>
@Html.DisplayNameFor(model => model.UserId)
</th>
...........................
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@item.RequestId
</td>
<td>
@item.UserId
</td>
..................
</tr>
}
</table>
<a asp-action="AddReqToHistory" asp-controller="ReqHistory" type="submit" value="Save" asp-route-HEREID="@model.ID" />
<a asp-action="EliminateReqFromHistory" asp-controller="ReqHistory" type="submit" value="Eliminate" asp-route-HEREID="@model.ID" />