【发布时间】:2019-04-24 14:10:40
【问题描述】:
我在尝试使自定义 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>
【问题讨论】:
-
'./ContextFreeGrammar/Add' 是什么意思?我们在您上面的代码中找不到该路线..
标签: c# asp.net-core