【发布时间】:2017-06-22 18:11:36
【问题描述】:
我的 .net Core MVC 项目中的表单助手遇到了一些问题。我有以下表格:
MyForm.cshtml
<div id="myDiv" class="row">
<br />
<form asp-controller="testing" asp-action="MyForm" asp-route-returnUrl="returnHome">
<input type="text" asp-for="Name" name="Name" />
<button type="submit">Submit</button>
</form>
以及我在 TestingController.cs 中的操作
[HttpGet, Route("testing/MyForm/{code}")]
public IActionResult MyForm(string code)
{
return View();
}
[HttpPost]
public IActionResult MyForm(FormVM request)
{
// do stuff
// return some view
return View();
}
这里的想法是用户使用某种代码(通常是 GUID,但也可以使用其他字符串)进入 MyForm 页面。然后他们填写表格并提交。我遇到的问题是表单的助手没有正确构建表单的操作(或者至少不是它所说的方式https://docs.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms)。
如果我使用上面的代码并检查 html,表单的操作将是“testing/MyForm/asdf?returnUrl=returnHome”(使用的代码是“asdf”)。
无论我是否包含 asp-controller 片段,我都会得到相同的结果。
如果我使用<form asp-route="MyForm">,那么表单的操作将是空白的,<form action method="post">
我需要的操作是“testing/MyForm?returnUrl=returnHome”,但我的 {code} 的值不断增加。有谁知道如何做到这一点?
谢谢!
【问题讨论】:
标签: asp.net-mvc forms asp.net-core html-helper