2017-08-08 185 views
0

我遇到ASP.NET BeginForm帮助程序的问题。ASP.NET HTML.BeginForm/Url.Action Url指向自己

我试图创建应指向/Project/Delete一种形式,我尝试了以下众所周知声明来实现这一目标:

@using (Html.BeginForm("Delete", "Project")) 
{ 
} 

<form action="@Url.Action("Delete", "Project")"></form> 

但不幸的是渲染操作点既/Projects/Delete/LocalSqlServer,这是网站的所谓的网址浏览器

<form action="/Project/Delete/LocalSqlServer" method="post"></form> 

我真的不知道为什么渲染的动作指向自身,而不是给出route.I已经阅读谷歌的所有帖子(我发现)和SO,但没有发现任何解决方案。

这是定义的唯一途径:

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。

我真的很感谢你的帮助。

感谢 桑德罗

+0

我碰到类似的东西,会看看我是否能找到你的github问题。 – nurdyguy

+0

你能告诉我们删除操作方法吗? – Win

+0

您需要显示您的路由定义以及'[HttGet]'和'[httpPost]'方法 –

回答

1

事实是,它是在asp.net一个错误,但他们拒绝承认它作为一个bug,只是称其为“功能”。但是,这里是你如何对待它...

这里是我的控制器是什么样子:

// 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获得。

下面是你如何做一个基本的ASP形式的两个版本:

@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 = "" }

希望这会有所帮助!

+0

此解决方案适用于我,谢谢@nurdyguy。 –