如何 post 从 Razor 页面到页面模型的非表单数据
How to post non-form data from Razor Page to Page Model
我刚刚开始进行 Web 编程(使用 Razor Pages),但在弄清楚如何 post 未直接在表单上输入的数据返回服务器时遇到了问题。我希望在没有 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>
在页面加载时,现有数据附加到 table。
模态弹出窗口用于 add/edit 数据,更新 table。
这是“提交”按钮的页面处理程序。
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; }
}
"display friendly" 属性在表单的 table 中以用户语言显示。非显示友好(语言中立)属性需要返回服务器进行处理。
由于 selection/filter 标准是 added/edited,所以维护了一个 javascript 数组 [named FilterTerms]。它在 table.
中存储每一行的完整信息
单击“提交”按钮后如何将数据返回到服务器。
谢谢!
将数据返回服务器的唯一方法是通过 post。因此,您要发送的所有数据都必须在表单中具有关联的输入,即使它是隐藏的。
唯一的其他选择是利用 JS 构建自定义 post 正文并通过 AJAX 发送。您仍然可以技术上 重定向,方法是在 AJAX 成功回调中更改 location
。但是,最好也更直接地使用传统形式 post,除非您确实需要保持在同一页面上。
更新
仔细查看您的代码后,我认为您的问题可能是您需要的数据仅供显示,实际上不应首先 post 返回。如果是这种情况,解决方案是在 post 上重建您的视图模型。如果有一些 属性(ies) 只用于显示,那么这些值应该每次都由首先设置它们的任何代码设置。不过,我需要查看更多您的代码才能为您提供更具体的指导。
我刚刚开始进行 Web 编程(使用 Razor Pages),但在弄清楚如何 post 未直接在表单上输入的数据返回服务器时遇到了问题。我希望在没有 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>
在页面加载时,现有数据附加到 table。 模态弹出窗口用于 add/edit 数据,更新 table。
这是“提交”按钮的页面处理程序。
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; }
}
"display friendly" 属性在表单的 table 中以用户语言显示。非显示友好(语言中立)属性需要返回服务器进行处理。
由于 selection/filter 标准是 added/edited,所以维护了一个 javascript 数组 [named FilterTerms]。它在 table.
中存储每一行的完整信息单击“提交”按钮后如何将数据返回到服务器。
谢谢!
将数据返回服务器的唯一方法是通过 post。因此,您要发送的所有数据都必须在表单中具有关联的输入,即使它是隐藏的。
唯一的其他选择是利用 JS 构建自定义 post 正文并通过 AJAX 发送。您仍然可以技术上 重定向,方法是在 AJAX 成功回调中更改 location
。但是,最好也更直接地使用传统形式 post,除非您确实需要保持在同一页面上。
更新
仔细查看您的代码后,我认为您的问题可能是您需要的数据仅供显示,实际上不应首先 post 返回。如果是这种情况,解决方案是在 post 上重建您的视图模型。如果有一些 属性(ies) 只用于显示,那么这些值应该每次都由首先设置它们的任何代码设置。不过,我需要查看更多您的代码才能为您提供更具体的指导。