Using Custom paths or absolute URI with Html.BeginForm()
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>
Best way to create form with Html.BeginForm() that should be SSL?
You can override the action attribute of the form created by Html.BeginForm.
<%
var actionURL = (Model.UseSSL ? "https://" : "http://")
+ Request.Url.Host + Request.Url.PathAndQuery;
using (
Html.BeginForm(
"Action",
"Controller",
FormMethod.Post,
new { @action = actionURL }
)
)
%>
Note the use of the Model.UseSSL flag, which should be passed to this View by its Controller.
t4Mvc and Html.BeginForm with split controller and action name
Please try:
Html.BeginForm(MVC.EmailTemplate.Save())
Getting Absolute URL from an ASP.NET MVC Action
You can do it by the following:
var urlBuilder =
new System.UriBuilder(Request.Url.AbsoluteUri)
{
Path = Url.Action("Action", "Controller"),
Query = null,
};
Uri uri = urlBuilder.Uri;
string url = urlBuilder.ToString();
// or urlBuilder.Uri.ToString()
Instead of Url.Action()
in this sample, you can also use Url.Content()
, or any routing method, or really just pass a path.
But if the URL does go to a Controller
Action
, there is a more compact way:
var contactUsUriString =
Url.Action("Contact-Us", "About",
routeValues: null /* specify if needed */,
protocol: Request.Url.Scheme /* This is the trick */);
The trick here is that once you specify the protocol
/scheme when calling any routing method, you get an absolute URL. I recommend this one when possible, but you also have the more generic way in the first example in case you need it.
I have blogged about it in details here:
http://gurustop.net/blog/2012/03/23/writing-absolute-urls-to-other-actions-in-asp-net-mvc/
Extracted from Meligy’s AngularJS & Web Dev Goodies Newsletter
Cannot assign attributes to form using Html.BeginForm
Your current code is matching with the below overload of BeginForm method
public static MvcForm BeginForm(
this HtmlHelper htmlHelper,
string actionName,
string controllerName,
object routeValues
)
The third parameter here is an object for the route values. These will be added as the querystring key value(s) to your form's action
attribute value. That is the reason you are seeing those big url as the action attribute value.
If you want to specify the html attributes( Id,class etc), Use this overload which has a fourth parameter which takes the html attributes. The third parameter is the FormMethod.
public static MvcForm BeginForm(
this HtmlHelper htmlHelper,
string actionName,
string controllerName,
FormMethod method,
object htmlAttributes
)
This should work.
@using (Html.BeginForm("Create", "Post",FormMethod.Post,
new { @id = "MaintenanceForm", @class = "datatable", @nonvalidate = "nonvalidate" }))
{
}
Replace Create
and Post
with your action method name and controller name.
Html.BeginForm does not redirect to action Method
Your Code should be like this then it will work
@using (Html.BeginForm("SubmitEmp","Client",FormMethod.Post))
We cannot and use full controller name like ClientController for specifying controller name we need to specify only the name of Client
In your URL localhost:5000/ClientController/Submitemp
showing controller name ClientController it should be Client only change your URL like localhost:5000/Client/Submitemp
then it will work.
Cheers!!
Rewriting Html.BeginForm() in MVC 3.0 and keeping unobtrusive javascript
The answer to your underlying question (i.e. how to use BeginForm/EndForm syntax) is to do it in the following manner:
@{ Html.BeginForm(...); }
<div> content</div>
@{ Html.EndForm(); }
Unfortunately the Razor syntax right now is a bit more verbose when invoking helpers that write to the output (as opposed to the majority of helpers which just return an html snippet). You could probably make this easier by writing your own extension methods like so:
public static IHtmlString FormBegin(this HtmlHelper helper, ...) {
helper.BeginForm(...);
return new HtmlString("");
}
public static IHtmlString FormEnd(this HtmlHelper helper) {
helper.EndForm();
return new HtmlString("");
}
Related Topics
How to Hide The Arrow That Is Displayed by Default on The HTML5 <Details> Element in Chrome
How Does Justify-Items Work on Display:Block Elements
Make a Parent Div Webkit-Filter Not Affect Children
<Input Type="File"> Limit Selectable Files by Extensions
Box-Sizing: Border-Box => for Ie8
Bootstrap Row and Col Explanation
Always Show Vertical Scrollbar in <Select>
Context.Getimagedata() Operation Is Insecure
Why Does Perspective Changes Fixed Position in CSS
HTML - Why Boolean Attributes Do Not Have Boolean Value
Twitter Bootstrap: Column Re-Ordering for Full Width Divs
How Might I Force a Floating Div to Match The Height of Another Floating Div
Changing Image Src Depending on Screen Size
Is It Ok to Use <Strong> in Place of <B> Blindly