我有一個頁面,在那里我做了一個搜索。然后我可以對搜索的結果進行一些操作。 這里的頁面:
@page "{handler?}"
@model Pricelists.ListModel
<form>
<div><label><input autocomplete="off" type="checkbox" asp-for="OnlyEnabled" /> Only enabled</label> </div>
//搜索中使用的所有其他過濾器
<input type="submit" formmethod="get" asp-page-handler="Search" />
@if (Model.SearchResult.PaginatedResults != null)
{
<input type="hidden" asp-for="PageNumber" value="@Model.PageNumber" />
<input type="hidden" asp-for="TotalPages" value="@Model.TotalPages" />
<table>
<tr>
//標題的結果
</tr>
@foreach (var item in Model.SearchResult.PaginatedResults)
{
<tr>
//結果
</tr>
}
</table>
<標簽>頁面 @Model.PageNumber of @Model.TotalPages (Number of records: @Model.SearchResult.Count)</label>
<input type="submit" formmethod="get" asp-page-handler="Previous" value="< " />
<input type="submit" formmethod="get" asp-page-handler="Next" value=">" />
<div>
<input type="submit" formmethod="post" asp-page-handler="Enable" value="Enable" />
<input type="submit" formmethod="post" asp-page-handler="Disable" value="Disable" />
<input type="submit" formmethod="post" asp-page-handler="Delete" value="Delete" />
</div>
}
</form>
正如你所看到的,無論是當我進行搜索還是當我改變頁面時,我都會使用GET提交表單。
但是我想做一個POST提交來修改(啟用、禁用或洗掉)搜索結果。
這里是PageModel:
public class ListModel : PageModel
{
private const int RECORDS_PER_PAGE = 20;
private readonly AdminApiClient _client;
#region Selected Filters
[BindProperty(SupportsGet=true)]。
public bool OnlyEnabled { get; set; }
[BindProperty(SupportsGet=true)] 公共 bool OnlyEnabled { get; set; }
public long[] SelectedCategories { get; set; }
[BindProperty(SupportsGet = true)] 公共字串[] 選定的運營商 { get; set; }
public string[] SelectedCarriers { get; set; }
[BindProperty(SupportsGet = true)] 公共字串[] SelectDepartures { get; set; }
public string[] SelectedDepartures { get; set; }
[BindProperty(SupportsGet = true)] 公共字串[] SelectDepartures { get; set; }
公共字串[] SelectArrivals { get; set; }
[BindProperty(SupportsGet = true)] 公眾的int PageNumber { get; set; }
public int PageNumber { get; set; }
[BindProperty(SupportsGet = true)] [BindProperty(SupportsGet = true)
public int TotalPages { get; set; }
#endregion
[BindProperty(SupportsGet = true)] 公眾號PriceListSearchViewModel的搜索模型。
public PriceListSearchViewModel SearchResult { get; set; }
public ListModel(AdminApiClient clientFactory)
{
_client = clientFactory;
}
public async Task OnGet()
{
await this.LoadViewDataAsync()。
}
public async Task OnGetSearch()
{
this.PageNumber = 1;
await this.LoadViewDataAsync();
this.SearchResult = await this._client.Pricelists.Search(this.SelectedCategories, this.SelectedCarriers, this.SelectedDepartures, this.SelectedArrivals, this.OnlyEnabled, this.PageNumber, RECORDS_PER_PAGE) 。
this.TotalPages = (int)Math.Ceiling(this.SearchResult.Count / (十進制)RECORDS_PER_PAGE)。
}
public async Task OnGetNext()
{
...
}
public async Task OnGetPrevious()
{
...
}
public async Task OnPostEnable()
{
await this.LoadViewDataAsync();
await this._client.Pricelists.ChangeStatus(this.SelectedCategories, this.SelectedCarriers, this.SelectedDepartures, this.SelectedArrivals, this.OnlyEnabled, this.PageNumber, RECORDS_PER_PAGE, enable: true) 。
}
public async Task OnPostDisable()
{
await this.LoadViewDataAsync();
await this._client.Pricelists.ChangeStatus(this.SelectedCategories, this.SelectedCarriers, this.SelectedDepartures, this.SelectedArrivals, this.OnlyEnabled, this.PageNumber, RECORDS_PER_PAGE, enable: false) 。
}
public async Task OnPostDelete()
{
await this.LoadViewDataAsync();
await this._client.Pricelists.Delete(this.SelectedCategories, this.SelectedCarriers, this.SelectedDepartures, this.SelectedArrivals, this.OnlyEnabled, this.PageNumber, RECORDS_PER_PAGE) 。
}
}
為了簡化,我讓你只看到一個GET和一個POST方法。但是,在任何情況下,我都使用相同的系結屬性。
現在,當我做一個GET提交時,一切都正常作業。但是當我進行POST提交時,我得到一個錯誤400 Bad request。我讓你看到一個fiddler:
http請求是我所期望的,但顯然不是回應。
有什么想法?
有什么想法嗎?
編輯OnPost*的方法。這就像我實作提交按鈕的方式是錯誤的。問題不在于OnPost*方法的實作。
謝謝你
uj5u.com熱心網友回復:
嘗試去掉你所擁有的表單,為每個按鈕做一個特殊的表單
<form asp-page-handler="delete" method="post" >
<button class="btn btn-default">洗掉</button>。
</form>
....,等等。
uj5u.com熱心網友回復:
你有兩個選擇,分離表單并為每個方法寫一個表單。或者直接用javascript處理。
看這個例子:
<form id="frm" action="/test" method="post" >
<按鈕型別="提交">保存</按鈕>。
和腳本:
<script>
setTimeout(function () {
var frmElement = document.getElementById('frm');
frmElement.setAttribute('method', 'get')
}, 2000)
</script>
uj5u.com熱心網友回復:
你可以嘗試在<form>中添加method="post"。例如:<form method="post">。
我用你的代碼進行了測驗,當點擊帶有formmethod="post"的輸入時,它將在表單資料中丟失__RequestVerificationToken。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/320475.html
標籤:



