开发者

Using Custom paths or absolute URI with Html.BeginForm() [duplicate]

This question already has answers开发者_运维问答 here: Closed 11 years ago.

Possible Duplicate:

Html.BeginForm() with an absolute URL?

How to put and absolute URI in the action attribute of the <form> tag generated by the Html.BeginForm() MVC3 Html helper. I am using MVC3 validation and do not want to lose it by writing my own Form tags.

I tried out both Html.BeginForm(new { action ="http://absolute.com"}) and Html.BeginForm(new { @action ="http://absolute.com"}) and the rendered html was <form action="/Pro/Contact/http%3a/absolute.com/">. As you can see it is being appended instead of replaced.


BeginForm method with single parameter is used to generate a relative path from routedata that is provided within parameter variable. Which means, that you are setting URI parameter with name action and giving it and value http://absolute.com, therefor value is going to be encoded.

What you want to use is overload where it asks you for htmlAttributes.

This will not encode value for action attribute:

@using (Html.BeginForm(
     null, null, FormMethod.Post,
     new {@action="http://absolute.com/submit/example"}
)){}
// the result is:
<form action="http://absolute.com/submit/example" method="post"> 
</form>

UPDATE: method show below will not utilize JavaScript client-side validation.

since, you don't really need the helper to figure out the path of the form. You can use the html and set action of the form manually.

Validation will still work, if you are using helpers for the input fields.

<form action="http://absolute.com/submit/example" method="post">    
    @Html.LabelFor(model => model.Example)
    @Html.ValidationMessageFor(model => model.Example, "*")
    @Html.TextAreaFor(model => model.Example, 8, 60, null)
</form>


One way is..we can use RedirectResult("http://absolute.com"); in the controller action method.

If you are redirecting from your controller (or action filter, etc.) you can use the RedirectResult as your ActionResult type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜