Html.BeginForm()具有绝对URL吗?

时间:2022-12-06 21:37:24

I need to have the POST action be to an absolute URL (e.g., http://www.cnn.com). Is there a way to use Html.BeginForm() helper and pass it the url?

我需要将POST动作设置为一个绝对URL(例如http://www.cnn.com)。是否有一种方法可以使用Html.BeginForm() helper并将url传递给它?

3 个解决方案

#1


60  

BeginForm method has several overloads. In order to set the action attribute on the form tag with the desired url, you need to use following overload of BeginForm:

BeginForm方法有多个重载。为了用所需的url设置表单标记上的action属性,需要使用BeginForm的如下重载:

BeginForm(String, String, FormMethod, IDictionary<String, Object>)
// here are the parameter names:
BeginForm(actionName, controllerName, method, htmlAttributes)

Since you want to post to an external site, there is no need to set actionName and controllerName, just leave them as null.

由于您希望发布到外部站点,所以不需要设置actionName和controllerName,只需将它们设置为null即可。

@Html.BeginForm(
   null, null, FormMethod.Post, new {@action="http://cnn.com/post"}
)

This will not encode the action parameter.

这将不会对动作参数进行编码。

#2


15  

All that the HtmlHelper.BeginForm method does is help you to create a <form> tag that targets a local controller. If you're posting to an external site, just write out the actual <form> tag, i.e.

所有HtmlHelper。BeginForm方法帮助您创建针对本地控制器的

标记。如果要发布到外部站点,只需写出实际的标记,即。

<form action="http://www.example.com/someaction" method="post">
    Actual form content in here
</form>

That's all there is to it. MVC forms are not like the forms in ASP.NET WebForms where you have a bunch of ViewState and event fields and other magical elements. They're just regular old HTML forms.

这就是一切。MVC表单不像ASP中的表单。NET WebForms,你有一堆ViewState和事件字段以及其他魔法元素。它们只是普通的旧HTML表单。

#3


0  

check the mvc source code, html.beginform method not only create the native html form only, but also add sth for client validation which just i want,

检查mvc源代码,html。beginform方法不仅只创建本机html表单,而且还添加一些用于客户验证的东西,这正是我想要的,

if (htmlHelper.ViewContext.ClientValidationEnabled) 
{
    htmlHelper.ViewContext.FormContext.FormId = tagBuilder.Attributes["id"];
}

so it will be a problem, i just write my own extension

这将是一个问题,我只写我自己的扩展

#1


60  

BeginForm method has several overloads. In order to set the action attribute on the form tag with the desired url, you need to use following overload of BeginForm:

BeginForm方法有多个重载。为了用所需的url设置表单标记上的action属性,需要使用BeginForm的如下重载:

BeginForm(String, String, FormMethod, IDictionary<String, Object>)
// here are the parameter names:
BeginForm(actionName, controllerName, method, htmlAttributes)

Since you want to post to an external site, there is no need to set actionName and controllerName, just leave them as null.

由于您希望发布到外部站点,所以不需要设置actionName和controllerName,只需将它们设置为null即可。

@Html.BeginForm(
   null, null, FormMethod.Post, new {@action="http://cnn.com/post"}
)

This will not encode the action parameter.

这将不会对动作参数进行编码。

#2


15  

All that the HtmlHelper.BeginForm method does is help you to create a <form> tag that targets a local controller. If you're posting to an external site, just write out the actual <form> tag, i.e.

所有HtmlHelper。BeginForm方法帮助您创建针对本地控制器的

标记。如果要发布到外部站点,只需写出实际的标记,即。

<form action="http://www.example.com/someaction" method="post">
    Actual form content in here
</form>

That's all there is to it. MVC forms are not like the forms in ASP.NET WebForms where you have a bunch of ViewState and event fields and other magical elements. They're just regular old HTML forms.

这就是一切。MVC表单不像ASP中的表单。NET WebForms,你有一堆ViewState和事件字段以及其他魔法元素。它们只是普通的旧HTML表单。

#3


0  

check the mvc source code, html.beginform method not only create the native html form only, but also add sth for client validation which just i want,

检查mvc源代码,html。beginform方法不仅只创建本机html表单,而且还添加一些用于客户验证的东西,这正是我想要的,

if (htmlHelper.ViewContext.ClientValidationEnabled) 
{
    htmlHelper.ViewContext.FormContext.FormId = tagBuilder.Attributes["id"];
}

so it will be a problem, i just write my own extension

这将是一个问题,我只写我自己的扩展