HTML.BeginForm not Routing to Controller Method - asp.net-mvc

I'm writing an MVC website in C# with razor syntax.
I have some code here:
#using (Html.BeginForm("DoSignUp", "SignUpController", FormMethod.Post))
{
<input type="text" width="3000" value="" name="Name" />
<input type="submit" value="Register" /
}
So this form should post to the SignUpController on the DoSignUp method. Instead of doing that it just posts back to the page I'm currently on which is called SignUp. I suspected there might be something wrong with my routing but when I look at the html that is being generated through the browser developer tools i see this:
<form action="" method="post">
<input type="text" width="3000" value="" name="selectedURL">
<input type="submit" value="Register">
</form>
Edit: Well I wrote this out and then looked at my code again. The problem which I keep bloody doing is that SignUpController should just be SignUp.

Remove the 'Controller' from your controller name (SignUp, not SignUpController). It's assumed to be a controller by convention
#using (Html.BeginForm("DoSignUp", "SignUp", FormMethod.Post))
{
<input type="text" width="3000" value="" name="Name" />
<input type="submit" value="Register" /
}

Related

Is it possible to include a file with a form POST

I got a regular POST in my mvc app:
#using (Html.BeginForm("Update", "AController", FormMethod.Post))
{
<div style="margin-bottom:20px"></div>
#Html.AntiForgeryToken()
<div class="form-input">
<input class="form-control" type="text" name="#(nameof(Model.Name))" value="#(Model.Name)" />
</div>
<br />
<div class="form-input">
<input class="form-control" type="text" name="#(nameof(Model.Name2))" value="#(Model.Name2)" />
</div>
<br />
<div>
<input type="file" name="Image" id="Image" name="#(nameof(Model.Image))" value="#(Model.Image)" />
</div>
<button type="submit" class="btn btn-primary btn-sm">TEXT HERE</button>
}
Now when doing the submit the two regular text fields gets binded the model but the file does not.
After some reading it seems like the upload part should be in a separate form, is that correct?
I really want to avoid having two different POST as it does not fit the design of the page.
You should be able to get the posted file from the Request directly in the controller with following code
if(Request.Files.Count > 0)
{
var file = Request.Files[0];
...
}
But you need to add the multipart/form-data to your form
#using (Html.BeginForm("Update", "AController", FormMethod.Post, new { enctype="multipart/form-data" }))
Check What does enctype='multipart/form-data' mean? for reference on the multipart/form-data

Get method : form input not in url

I am doing my first ASP.NET mvc project, on the home page, Index.cshtml, I have a small form:
<form action="ChoixFormulaire" method="get">
<fieldset>
<label>NAS</label>
<input id="nas" type="text" placeholder="###"/>
<br />
<label>Date of birth</label>
<input id="date" type="text" placeholder="AAAA-MM-JJ"/>
<br />
<label>Employee number</label>
<input id="numEmployee" type="text" placeholder="######"/>
<br />
</fieldset>
<input type="submit" value="Soumettre" onclick="return VerifierFormulaire()" />
</form>
When the button is clicked, there is some verification made in the 'VerifierFormulaire()' method, which is defined in the same Index.cshtml file. Then the ChoixFormulaire.cshtml is displayed (called from the ChoixFormulaire() method in my HomeController, which returns View()).
I was expecting the form inputs to be in the URL as parameters. For example, If I enter '123' for NAS, '1989-01-01' for date of birth and '123456' for employee number, I am redirected to http://localhost:15778/Home/ChoixFormulaire? but I would expect to be redirected to http://localhost:15778/Home/ChoixFormulaire?nas=123&dateBirth=1989-01-01&numEmployee=123456
Try adding the name attribute:
<input id="nas" name="nas" />

Form string Post redirection in MVC

I was trying to integrate Payumoney Payment gateway in one of a E Commerce site developed in MVC4. To redirect to the gateway for payment I have to submit :
<form id="PostForm" name="PostForm" action="https://test.payu.in/_payment" method="POST">
<input type="hidden" name="lastname" value="">
<input type="hidden" name="address2" value="">
<input type="hidden" name="udf5" value="">
<input type="hidden" name="curl" value="">
<input type="hidden" language='javascript'>
var vPostForm = document.PostForm;
vPostForm.submit();
</script>
In ASP.Net, this is the code
Page.Controls.Add(new LiteralControl(strForm));
How to achieve this in MVC?
Thanks,
Prasant
Page.Controls.Add(new LiteralControl(strForm));
Instead of above line of code just write :
Response.Write(strForm);
Create it as partial. Put this markup in its view. And in your main view code use #Html.RenderPartial("Payment")

RazorGenerator not processing Html.EditorFor or Html.BeginForm

I have the following razor code:
<form id="logon" action="/security/dev" method="get">
#Html.AntiForgeryToken()
#Html.EditorFor(x => x.UserName)
#Html.EditorFor(x => x.Password)
<input type="submit" value="Login" name="Login" />
<input type="submit" value="Cancel" name="Cancel" />
</form>
when getting the precompiled version to output the generated html I get the following:
<form id="logon" action="/security/dev" method="get">
<input name="__RequestVerificationToken" type="hidden" value="vTyswnqonYMzGeewLrLSJ9XySz1A0PR0nvyVu58458J/nftXtxBPIVoQEdfr3MzEYPDLBPcGvXtMkOTujsou/x3eJVfdt2YSJgxUfu6AxMLj23kTwUNQo7X8ec7twsbt8U2BdogpHy0fSGq1nMljlukM9fGZ/770JLijcpJXx4o=" />
/* EditorTemplates/String */
/* EditorTemplates/Password */
<input type="submit" value="Login" name="Login" />
<input type="submit" value="Cancel" name="Cancel" />
</form>
Anyone have any idea why it would do that? Here is a failing example: skydrive file
Cheers
w://
Apparently this is not part of the expected output.
I had similar problem with EditFor. Using TextBoxFor instead helped me.

Do MVC3 non-sequential hidden input indexes need to come first?

MVC3 non-sequential index hidden inputs for model binding..
<input type="hidden" name="Index" value="whatever" />
Does it matter if they go before, after, in the middle of the other related inputs to be posted?
Does it matter at all where they end up in the posted data?
For example, can they all be lumped together and it still works?
<input type="text" name="[A].Id" value="1" />
<input type="text" name="[B].Id" value="2" />
<input type="hidden" name="Index" value="A" />
<input type="hidden" name="Index" value="B" />
No, the order of your form fields does not matter, nore where they appear on the html page.
The most important factor for MVC3 is the name of the fields must match to the name of your controller/action parameter.
If you have two fields with the same name however, only one value will be returned into your action.
As long as the hidden fields are located inside of the form it should not matter the order in which they are placed. Please see code sample below. Notice how the hidden fields are put anywhere inside of the form.
#using (Html.BeginForm())
{
#Html.ValidationSummary(false, "Please correct the following errors")
#Html.HiddenFor(m => m.CoolStuffId)
#Html.Partial("_EditCoolStuff", Model)
<fieldset class="ui-grid-a">
<div class="ui-block-a"><a data-role="button" href="#Url.Action("ActionPlan", "Store", new { id = Model.StoreID })">Cancel</a></div>
<div class="ui-block-b"><button type="submit" data-theme="a">Submit</button></div>
</fieldset>
#Html.HiddenFor(m => m.TypeId)
}

Resources