调用Html。BeginForm和指定的行动

时间:2022-12-06 21:46:46

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?

有人知道怎么做吗?

3 个解决方案

#1


16  

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.

有关可用过载的完整列表,请参阅文档。

#2


13  

Perhaps try this:

也许试试这个:

@using (Html.BeginForm("action", "controller"))

This uses the correct overload and has a simpler (imo) syntax.

这使用了正确的重载并具有更简单的语法(imo)。

#3


0  

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模型= 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并忽略它来解决这个问题(哇!)或者通过传递参数而不是使用路由(可能是最好的),但是有时也有外部原因,这不是一个选项,您必须处理现有的情况。

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)
{
    ...
}

#1


16  

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.

有关可用过载的完整列表,请参阅文档。

#2


13  

Perhaps try this:

也许试试这个:

@using (Html.BeginForm("action", "controller"))

This uses the correct overload and has a simpler (imo) syntax.

这使用了正确的重载并具有更简单的语法(imo)。

#3


0  

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模型= 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并忽略它来解决这个问题(哇!)或者通过传递参数而不是使用路由(可能是最好的),但是有时也有外部原因,这不是一个选项,您必须处理现有的情况。

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)
{
    ...
}