asp.net-mvc 调用 Html.BeginForm 并指定操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6020253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling Html.BeginForm and specifying action
提问by Marcel
I have been trying to do the following but whatever I try I just keep getting different errors:
我一直在尝试执行以下操作,但无论我尝试什么,我都会不断收到不同的错误:
@using (Html.BeginForm(Url.Action(this.ViewContext.RouteData.Values["action"] as string)))
This for example produces:
这例如产生:
<form action="/adminTests/create?Length=22" method="post">
Has anyone figured out how to do this?
有没有人想出如何做到这一点?
回答by Darin Dimitrov
You are using a wrong overload. It should be:
您正在使用错误的重载。它应该是:
@using (Html.BeginForm(new { action = ViewContext.RouteData.Values.GetRequiredString("action") }))
or:
或者:
@using (Html.BeginForm(ViewContext.RouteData.Values.GetRequiredString("action"), ViewContext.RouteData.Values.GetRequiredString("controller"))))
or if you want to generate a form POSTing to the current url use simply (note that this will include any query string parameters):
或者,如果您想生成一个发布到当前 url 的表单,请简单地使用(请注意,这将包括任何查询字符串参数):
@using (Html.BeginForm())
For full list of available overloads consult the documentation.
有关可用重载的完整列表,请参阅文档。
回答by Legoguy
Perhaps try this:
也许试试这个:
@using (Html.BeginForm("action", "controller"))
This uses the correct overload and has a simpler (imo) syntax.
这使用了正确的重载并具有更简单的 (imo) 语法。
回答by Red
Edge case answer
边缘情况答案
If you are posting back to an action of the same name, but you loaded the form with more data from the route you have to do one more thing. In your controller, you need to remove the extra routing values.
如果您要回发到同名的操作,但您从路由中加载了更多数据的表单,则您必须再做一件事。在您的控制器中,您需要删除额外的路由值。
RouteData.Values.Remove("CompanyId");
Let me explain.
让我解释。
In my case I was loading the Add Contact form passing it the company id in the route, e.g., contact/add/123
to the Add action public IActionResult Add(int companyId)
. The Add/Edit contact form is then loaded with a new ViewModel with the CompanyId populated. var model = new ContactViewModel { CompanyId = companyId };
在我的例子中,我正在加载 Add Contact 表单,将路由中的公司 ID 传递给它,例如,contact/add/123
到 Add action public IActionResult Add(int companyId)
。然后,添加/编辑联系人表单会加载一个新的 ViewModel,其中填充了 CompanyId。var model = new ContactViewModel { CompanyId = companyId };
The route now serves no purpose, but if you try to use Html.BeginForm("add", "contact")
the form will still have an action of contact/add/123
, which will cause the routing to fail.
路由现在没有任何用处,但是如果您尝试使用Html.BeginForm("add", "contact")
该表单仍然会有 的操作contact/add/123
,这将导致路由失败。
You can work around this by taking the id on the add and ignoring it (yuk!). Or by passing parameters rather than using routing (probably best), but sometimes there are external reasons that's not an option and you have to work with the situation you have.
您可以通过在 add 上获取 id 并忽略它来解决这个问题(yuk!)。或者通过传递参数而不是使用路由(可能是最好的),但有时有外部原因不是一种选择,您必须处理您遇到的情况。
The Action in one go for easier reading
一键式操作,阅读更轻松
[HttpGet]
[Route("add/{companyId}")]
public IActionResult Add(int companyId)
{
var model = new ContactViewModel { CompanyId = companyId };
RouteData.Values.Remove("companyId");
return PartialView("_ContactModal", model);
}
[HttpPost]
[Route("add")]
public IActionResult Add(ContactViewModel model)
{
...
}