【问题标题】:How to take data from a view and send it to a controller action? ASP.NET Core MVC如何从视图中获取数据并将其发送到控制器操作? ASP.NET 核心 MVC
【发布时间】:2020-08-22 16:42:27
【问题描述】:

我有一个视图,其中显示了 Requests 表中的所有记录。每条记录都有一个保存和删除按钮,因此用户可以向/从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>
}

这是来自ReqHistoryController的两个操作:

public async Task<IActionResult> AddReqToHistory([Bind("ID, Title,Description,ImagePath")] Request model)
{
    var currentUser = await _userManager.GetUserAsync(User);
    var userId = await _userManager.GetUserIdAsync(currentUser);

    var newReqHistory = new ReqHistory()
        {
                UserId = userId,
                RequestId = model.ID,
                Title = model.Title,
                Description = model.Description,
                CategoryID = model.CategoryID,
                PersonID = model.PersonID,
                ImagePath = model.ImagePath
        };

    _context.Add(newReqHistory);
    await _context.SaveChangesAsync();
    return RedirectToAction("Index", "Requests");
}

// POST
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> EliminateReqFromHistory(int requestId)
{
    var reqHistory = await _context.ReqHistory.FindAsync(_userManager.GetUserId(User), requestId); //ReqHistory has a composite PK
    _context.ReqHistory.Remove(reqHistory);
    await _context.SaveChangesAsync();

    return RedirectToAction("Index", "Requests");
}

ReqHistory模型类:

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
}

这是ReqHistory 视图:

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

【问题讨论】:

  • 这只是基本的 HTML 101。您需要一个或多个带有隐藏输入数据的 HTML 表单,或者您需要使用 AJAX 发出请求并通过选择数据来动态构建帖子正文这页纸。无论哪种情况,关键是您必须以可以发送的方式实际提供数据。页面上的随机数据不仅仅是自动发送的。那没有任何意义。
  • 我知道这是一个愚蠢的问题,但我是初学者,我还在学习。谢谢您的回答!祝你有美好的一天!
  • 明白。我的观点是,您需要学习 HTML 和 JavaScript 之类的基础知识。当您不了解基本 Web 技术的工作原理时,您不能直接跳入 Web 应用程序。

标签: entity-framework-core asp.net-core-mvc


【解决方案1】:
        <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" />

你可以通过 url 方法通过 asp-route 传递 id。

【讨论】:

    猜你喜欢
    • 2014-03-07
    • 1970-01-01
    • 2012-02-22
    • 2020-08-26
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 2014-03-23
    相关资源
    最近更新 更多