【问题标题】:Asp form helpers not creating route correctlyAsp 表单助手没有正确创建路由
【发布时间】: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 片段,我都会得到相同的结果。

如果我使用&lt;form asp-route="MyForm"&gt;,那么表单的操作将是空白的,&lt;form action method="post"&gt;

我需要的操作是“testing/MyForm?returnUrl=returnHome”,但我的 {code} 的值不断增加。有谁知道如何做到这一点?

谢谢!

【问题讨论】:

    标签: asp.net-mvc forms asp.net-core html-helper


    【解决方案1】:

    我在这里找到了答案:https://github.com/aspnet/Mvc/issues/6420

    我不喜欢它,但答案是故意杀死有问题的变量。在我的情况下,使用

    <form asp-controller="testing" asp-action="MyForm" asp-route-code="" 
                                              asp-route-returnUrl="returnHome">
    

    成功了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-04
      • 1970-01-01
      • 2013-02-10
      • 2016-04-03
      • 1970-01-01
      • 2016-09-30
      • 2020-07-29
      • 1970-01-01
      相关资源
      最近更新 更多