【问题标题】:How to post non-form data from Razor Page to Page Model如何将非表单数据从 Razor 页面发布到页面模型
【发布时间】:2019-08-02 02:10:52
【问题描述】:

我刚开始使用 Web 编程(使用 Razor 页面),在弄清楚如何将未直接输入表单的数据发布回服务器时遇到了问题。我希望在没有 Ajax 的情况下这样做,因为我需要在处理数据后重定向。

这是 [.cshtml] 表单的一部分

@page
@model TaiRoxWeb.Models.FilterModel
@{
    ViewData["Title"] = "Filter";
}

<h1>@ViewData["Title"]</h1>

<form method="post">
    <div class="card">
        <div class="card-header">
            Filter Criteria
        </div>
        <div class="card-body">
            <table class="table table-sm" id="tblFilter">
                <thead>
                    <tr>
                        <th>
                            Line
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.FilterTerms[0].DisplayFieldName)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.FilterTerms[0].DisplayOperation)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.FilterTerms[0].DisplayValues)
                        </th>
                        <th>

                        </th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
            <div class="text-left">
                <button type="button" class="btn btn-sm" id="btnAddLine">Add...</button>
                <button type="button" class="btn btn-sm" id="btnClearAll">Clear</button>
            </div>
        </div>
    </div>
    <br />

    <button type="submit" class="btn btn-sm btn-secondary" asp-page-handler="Cancel" asp-route-PageName="@Model.PageName">Cancel</button>
    <button type="submit" class="btn btn-sm btn-primary" asp-page-handler="Submit" asp-route-PageName="@Model.PageName" asp-route-FilterTerms="FilterTerms">Submit</button>
</form>

在页面加载时,现有数据会附加到表中。 模式弹出窗口用于添加/编辑数据,从而更新表格。

这是提交按钮的页面处理程序。

public ActionResult OnPostSubmit([FromForm] List<SelectTerm> filter)
{
    if (!ModelState.IsValid)
    {
        return Page();
    }
    return RedirectToPage(@"/" + PageName);
}

型号

public class SelectTerm
{
    public int Id { get; set; }

    public string FieldName { get; set; } // select field's Name

    /// <summary>
    /// This is the display-friendly form of FieldName that is shown in the selection criteria grid.
    /// </summary>
    [Display(Name = "Field")]
    public string DisplayFieldName { get; set; } // select field's DisplayName

    public SelectOperation Operation { get; set; }

    /// <summary>
    /// This is the display-friendly form of Operation that is shown in the selection criteria grid.
    /// </summary>
    [Display(Name = "Operation")]
    public string DisplayOperation { get; set; }

    /// <summary>
    /// Gets or sets Values.
    /// This is a list of strings in order to support the "Is One Of" operation.
    /// All filter types except "Is One Of" use only the first item in the list.
    /// </summary>
    public List<string> Values { get; set; }

    /// <summary>
    /// This is the display-friendly form of Values that is shown in the selection criteria grid.
    /// </summary>
    [Display(Name = "Value(s)")]
    public string DisplayValues { get; set; }
}

“显示友好”属性以用户语言显示在表单的表格中。非显示友好(语言中立)属性需要返回服务器进行处理。

在添加/编辑选择/过滤条件时,将维护一个 JavaScript 数组 [名为 FilterTerms]。它存储表中每一行的完整内容。

点击提交按钮后如何将数据返回到服务器。

谢谢!

【问题讨论】:

  • 你看过@Html.HiddenFor吗?
  • 是的,但仍然坚持要求表单上的输入字段。模型属性之一是列表/数组,因此模型绑定有点棘手。

标签: asp.net-core asp.net-core-2.0


【解决方案1】:

将数据返回到服务器的唯一方法是通过帖子。因此,您要发送的所有数据都必须在表单中具有关联的输入,即使它是隐藏的。

唯一的其他选择是利用 JS 构建自定义帖子正文并通过 AJAX 发送。您仍然可以技术上通过在 AJAX 成功回调中更改 location 来重定向。但是,除非您确实需要保持在同一页面上,否则只使用传统的表单帖子会更好、更直接。

更新

仔细查看您的代码后,我认为您的问题可能是您需要的数据是仅显示的内容,实际上不应该首先发回。如果是这种情况,解决方案是在发布后重建您的视图模型。如果有一些仅用于显示的属性,那么每次都应该通过首先设置它们的任何代码来设置这些值。不过,我需要查看您的更多代码才能为您提供更具体的指导。

【讨论】:

  • 同意您对 Ajax 的评论。我已经尝试过仅将显示与所需数据分开,但仍然坚持需要表单上的输入字段。我希望有一种方法可以明确指定单击提交按钮时要发回的数据。
  • 输入是“在单击提交按钮时明确指定要发送回哪些数据的方式”,但是,您应该只发布允许用户更改的数据。其他一切都应该始终来自服务器。是的,这可能意味着再次进行数据库查询等。不,这不是问题。
  • 我会尝试这种方法。我的回发数据包含一个列表列表,因此担心标准模型绑定不够用。谢谢克里斯。
  • 通过将仅显示的数据与回发所需的数据分开,然后在表单上为回发数据创建隐藏的输入字段,我能够使模型绑定工作。再次感谢克里斯..
猜你喜欢
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 2018-09-28
  • 2010-09-17
  • 2010-11-20
  • 1970-01-01
  • 2015-05-05
  • 2020-03-20
相关资源
最近更新 更多