Html.BeginForm() with GET method - asp.net-mvc

How can I specify that my form should be using GET method with #Html.BeginForm() ?
#using (Html.BeginForm(method: FormMethod.Get))
Here VS complains that best overload doesn't have a parameter method. Thank you!

There is an overload that allows you to specify the method:
#using (Html.BeginForm("someAction", "someController", FormMethod.Get))
{
...
}

Decorate the controller's action method with [HttpGet]. This is the controller action that this form will submit to.

Related

Html.BeginForm with overload causes form action to be '/cgi-bin?action=Index&controller=Home' and so the HomeController is bypassed

Because I wanted to assign an id to a form element my code was #using (Html.BeginForm(null, null, FormMethod.Post, new { id = "indexform"}))
This resulted in markup <form action="/cgi-bin?action=Index&controller=Home" id="indexform" method="post" style="position: relative;">
My HomeController ActionResult Index was completely bypassed.
Notice the '/cgi-bin'. This might be the problem and I think the culprit may be parameter 'FormMethod.Post' but I could not enter null there.
(BTW I worked around the id requirement by using jQuery var form = $("#btnShowProperty").closest("form");)
I did quite a bit of Googling on this with no luck.
The problem is you're passing null for the action and controller in the first two arguments. Instead, pass the name of your action and controller and it will work.
Example, if your action is "Index" and your controller is "Home", then use:
#using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "indexform"})) {
...
}
The problem is mixing web pages and mvc especially using MapPageRoute AND MapRoute in Route.config. See full answer #Url.Action(“Action”, “Controller”) returns “/cgi-bin?action=Action&controller=Controller”

Html.BeginForm inside partial changes attributes

In my ASP.NET MVC (5.2) project, I have a page called register.cshtml. It doesn't include any forms or anything, just plain divs.
Inside one of the divs, I'm rendering a partial:
#Html.Partial("~/Views/Users/_x.cshtml").
Inside _x.cshtml I have a form:
#using (Html.BeginForm("/users/x"))
{
...
}
When I go to my register page, I expect my form to be rendered as:
<form action="/users/x" method="post"> ... </form>
But instead, I'm getting this:
<form action="/users/register?Length=23" method="post" novalidate="novalidate"> ... </form>
What is length=23, why is there a novalidate attribute added, and why is it posting to an incorrect path?
Why is my form not rendering properly?
If your wanting to post to a method named x in usersController, then it needs to be
#using (Html.BeginForm("x", "users"))
{
....
}
Note that your currently using the overload that accepts object routeValues and because its a string, the method generated a route value for Length because that's the only property of string (the /users/register is because that the method that generated the main view)
From your code
Html.BeginForm("/users/x")
i understand that users your controller and x is a method. So you can do in this way-
#using (Html.BeginForm("x", "users", FormMethod.Post, new { id = "YourFormID"}))
{
}
#using (Html.BeginForm("action", "controller",new { QueryString = 1}, FormMethod.Post, null))
{
}
Note : its due to passing wrong parameter in beginform constructor .
and in ur VIEW
#Html.Partial("~/Views/Shared/_x.cshtml")

Httppost different named view and action method

I have a view called "page.chtml" and I want to post from it to an action called "actionname"
[HttpPost]
public ActionResult actioname(...){...}
Is it possible?
Yes you can. In the action property of the form in page.cshtml simply specify actionname:
<form action="actioname">
you can use Html helper for creating a form with your desire submit action
#using (Html.BeginForm("actionName", "ControllerName", FormMethod.Post, new { id = "FormId", name = "FormName" }))
{
<div>//your page.cshtml inner html code goes here
}
If you want to post a form to a different action name, you can use the HTML helper for that. In Razor syntax it looks like this:
#using (Html.BeginForm("actioname"))
{
<!-- Form Fields -->
}
There are a few parameters you can use - the action is one, another is the controller in case you want to post to an ActionResult in a different controller than the one that handled the request for the page initially.
Yes You can use actioname in different controllers and post it:
in any view in any controller you can post:
#using (Html.BeginForm("actioname", "Controller", FormMethod.Post))
{
}

Using Html.BeginForm with querystring

My url looks like this:
customer/login?ReturnUrl=home
In the login view, I have used this pattern of code which works fine.
#using(Html.BeginForm())
{
...
}
This magically generates following html
<form action="customer/login?ReturnUrl=home" method="post">
But now, I need to add an attribute (e.g., data-id="something") in the form. How can I do that? If I don't have any query string, I know I can do something like this:
#using(Html.BeginForm(action, controller, FormMethod.Post,
new { data_id="something" }))
But don't know how to add querystring which should be in html:
<form action="customer/login?ReturnUrl=home" method="post" data-id="something">
I thought about using <form> directly but don't know how to specify querystring which is variable. And I have no idea how to achieve it with Html.BeginForm. Any tip would be appreciated.
RESOLUTION:
For now, I used <form> with following hint How to get current url value in View. The resulting view looks like
<form action="#Request.Url.PathAndQuery" data-id="something" method="POST">
But it would be nice to have an overloaded method of BeginForm for this.
Here's The way that worked for me
Html.BeginForm("Profile", "Partner", routeValues: new {id=Partner.partner_id},method:FormMethod.Post)
It was almost like there was a problem with overloading the method, but by specifying what things are, it seems to work fine...
To create a RouteValueDictionary from the querystring:
RouteValueDictionary queryStringDictionary = new RouteValueDictionary(Request.QueryString.AllKeys.ToDictionary(key => key, key => (object)Request.QueryString[key]));
Then you can use it with Html.BeginForm:
Html.BeginForm(null, null, queryStringDictionary, FormMethod.Post, new Dictionary<string, object> { { "autocomplete", "off" } })
I guess this doesn't directly answer the question, but why not just use a plain old form tag?
<form action='customer/login?ReturnUrl=#Request.QueryString["ReturnUrl"]' method="post" data-id="something">
Alternatively, you can create a custom HtmlHelperExtension that renders a form with path and querystring. In this HtmlHelperExtension you can iterate through your querystring values and populate the routeValueDictionary which you then pass to a Html.BeginForm constructor.
If you don't want something so extensible you can just use the overloaded constructor of Html.BeginForm using
#Html.BeginForm("login", "customer", new {ReturnUrl = #Request.QueryString["ReturnUrl"]},FormMethod.Post, new {data-id="something"});
using Reflector to look at the code,
BeginForm() will pass directly the rawUrl over to the final Form.
Any other overloads on BeginForm will go through a helper utility which will strip the query string.
This works for me :
#using (Html.BeginForm("index", "Photos", routeValues: new { user = pUser, album = pAlbum, }, method: FormMethod.Get))
Explicit route values and method is what is required...
Just incase you wanted to add other attributes as well. use below code
#using (Html.BeginForm("actionName", "controllerName", routeValues: new { lang = "en" }, method:FormMethod.Post, htmlAttributes: new { #class= "my-form", enctype = "multipart/form-data" }))
Try #using(Html.BeginForm(null, null, FormMethod.Post, new { data_id="something" }))
It should use the default logic to construct the url, just as if you used BeginForm()
(never tried that though in such case, but I believe it should work)

Html.ActionLink URL error

return RedirecttoAction("Success")
how to generate redirection?
You are using the wrong overload of Html.ActionLink, you need
<%: Html.ActionLink("linkText", "actionName", "controllerName") %>
Without the third controllerName parameter it will default to the current controller which appears to be in this case your UserController when I expect you want to direct to the AccountController. That is why the ActionLink works in your other view.

Resources