c# - 如何请求服务器将项目添加到集合中?
问题描述
我在尝试使自定义 POST 请求正常工作时遇到问题。
到目前为止,我已经尝试了几种可能的解决方案,例如弄乱路由、使用表单和 asp 标签助手等,但我无法查明问题所在。
参考下面附加的代码,在提交表单时,我期望 OnPostAdd 方法将使用作为输入提供的适当的 To 和 From 值来调用。但是,页面只是重定向到“./ContextFreeGrammar/Add”而不执行发布请求。
OnGet() 和 OnPost() 在此页面模型中有效,但如何调用 AddRule(Rule rule)?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Earley_Parser.Language;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Http;
namespace Storyteller.Pages.EarleyParser {
public class ContextFreeGrammarModel : PageModel {
public readonly Grammar_2 _grammarContext;
public ContextFreeGrammarModel(IGrammar grammarContext) {
_grammarContext = (Grammar_2) grammarContext;
_grammarContext.Add("", "S");
_grammarContext.Add("A", "S");
_grammarContext.Add("AA", "A");
_grammarContext.Add("a", "A");
}
// POST: Default/Create
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult OnPostAdd(String to, String from) {
try {
// TODO: Add insert logic here
_grammarContext.Add(to, from);
return RedirectToPage();
} catch {
return Page();
}
}
// POST: Default/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult OnPostDelete(Int32 id) {
try {
// TODO: Add delete logic here
_grammarContext.Remove(_grammarContext.ElementAt(id));
return RedirectToPage();
} catch {
return Page();
}
}
}
}
@page
@model Storyteller.Pages.EarleyParser.ContextFreeGrammarModel
@section Scripts {
<script>
$(function () {
// jQuery methods go here...
$("#F").keyup(function () {
if (this.value.length == this.maxLength) {
$("#T").focus();
}
});
});
</script>
}
<div>
<form asp-action="add" method="post">
<table id="grammarRuleTable" class="table">
<tbody>
@foreach (var rule in Model._grammarContext) {
<tr>
<td style="text-align:center" class="oneCharacterWideColumn">
@(new String(rule.f))
</td>
<td class="oneCharacterWideColumn">
→
</td>
<td style="margin:0">
@(new String(rule.t))
</td>
<td class="oneCharacterWideColumn">
<button onclick="myFunction()">
<i class="material-icons">delete_forever</i>
</button>
</td>
</tr>
}
<tr>
<td class="oneCharacterWideColumn">
<input id="F" type="text" maxlength="1" size="1" style="text-align:center"
asp-route-to="from" />
</td>
<td class="oneCharacterWideColumn" width="">
→
</td>
<td>
<input id="T" type="text" maxlength="128" style="width:100%"
asp-route-to="to" />
</td>
<td class="oneCharacterWideColumn"></td>
</tr>
</tbody>
</table>
</form>
</div>
解决方案
您可以使用处理程序执行此操作。
public IActionResult OnPostAdd(String to, String from) {
try {
// TODO: Add insert logic here
_grammarContext.Add(to, from);
return RedirectToPage();
} catch {
return Page();
}
}
-
<form asp-page-handler="Add" method="POST">
<button type="submit" class="btn btn-default">Add Grammar</button>
<input id="handler_parameter_from" type="hidden" name="from" value="hello"/>
<input id="handler_parameter_to" type="hidden" name="to" value="world"/>
</form>
请注意名称 OnPost Handler或 OnPost Handler Async。
您甚至可以在每页使用多个处理程序
推荐阅读
- haskell - 在 Archlinux 上设置 Haskell 的建议方法是什么?
- javascript - 如何从 javascript 文件 (Node.js) 启动 java 文件
- html - HTML 视频中的边框
- r - 如何从多个文件夹中读取一个文件?
- sql - 将 Json 表中的数据插入到已经在 SQL 中用 exec 创建的视图中
- python - 用户模型 Django 中的自定义字段
- python - Airflow + Kubernetes Executor 资源版本太旧
- reactjs - React HOC 无法解析导入
- javascript - API 中的 React 搜索选项需要运行两次?
- r - 如何表达极低的数字