【发布时间】: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