ASP.NET HTML.BeginForm/Url.Action网址指向自身 [英] ASP.NET HTML.BeginForm/Url.Action Url points to itself
问题描述
我遇到了ASP.NET BeginForm
助手的问题.
I facing an issue with the ASP.NET BeginForm
helper.
我尝试创建一个指向/Project/Delete
的表单,并尝试了以下Statemant来实现此目标:
I try to create a form that should point to /Project/Delete
and I tried the following statemant to reach this target:
@using (Html.BeginForm("Delete", "Project"))
{
}
<form action="@Url.Action("Delete", "Project")"></form>
但是不幸的是,这两个呈现的动作都指向/Projects/Delete/LocalSqlServer
,这是在浏览器中调用的网站的网址
But unfortunately both rendered actions points to /Projects/Delete/LocalSqlServer
, which is the url of site called in the browser
<form action="/Project/Delete/LocalSqlServer" method="post"></form>
我真的不知道为什么渲染的动作指向自身而不是给定的路线.我已经阅读了google和SO上的所有帖子(找到了),但没有找到解决方法.
I really dont know why the rendered action points to itself instead of the given route.I already read all posts (which I found) on google and SO, but found no solution.
这是唯一定义的路由:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
这是我的控制器
[HttpGet]
public ActionResult Delete(string id)
{
return View(new DeleteViewModel { Name = id });
}
[HttpPost]
public ActionResult Delete(DeleteViewModel model)
{
_configService.DeleteConnectionString(model);
return null;
}
我正在使用.NET 4.6.2.
I am using .NET 4.6.2.
非常感谢您的帮助.
谢谢 桑德罗
推荐答案
真相是,它是asp.net中的错误,但他们拒绝承认它为错误,而只是将其称为功能".但是,这是您的处理方式...
Truth is, it is a bug in asp.net but they refuse to acknowledge it as a bug and just call it a "feature". But, here is how you deal with it...
这是我的控制器的外观:
Here is what my controller looks like:
// gets the form page
[HttpGet, Route("testing/MyForm/{code}")]
public IActionResult MyForm(string code)
{
return View();
}
// process the form submit
[HttpPost, Route("testing/MyForm")]
public IActionResult MyForm(FormVM request)
{
// do stuff
}
因此,在我的情况下,code
会被附加,就像您使用LocalSqlServer
时一样.
So in my case, the code
would get appended just like you are getting with the LocalSqlServer
.
以下是制作基本asp表单的两种版本:
Here are both versions of how you make a basic asp form:
@using(Html.BeginForm("myform", "testing", new {code = "" }))
{
<input type="text" value="123" />
}
<form id="theId" asp-controller="testing" asp-action="myform" asp-route-id="" asp-route-code="">
<input type="text" value="asdf" />
</form>
在我放置asp-route-code
的停靠点中,代码"需要与控制器中的变量匹配.与new {code = "" }
相同.
In the stop where I put asp-route-code
, the "code" needs to match the variable in the controller. Same for new {code = "" }
.
希望这会有所帮助!
这篇关于ASP.NET HTML.BeginForm/Url.Action网址指向自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!