I have a simple form which have name and age input boxes, at the bottom I have two buttons like:
<input type="submit" id="submitButton" name="submitAction" value="Save" />
<input type="submit" id="submitButton" name="submitAction" value="Submit" />
The save button just creates a temporary data so they can come back and edit it, submit button will go ahead and create it, now when any of the buttons is clicked it goes to this controller function:
public ActionResult CreatePerson(Person model, string submitAction)
{
//lots of code i removed
}
but the submitAction is null, any ideas on to why that is happening?
My full .cshtml code
#model Person
#using (Html.BeginFormAntiForgeryPost(null, FormMethod.Post))
{
#Html.TextBox(x => x.Name)
#Html.TextBox(x => x.Age)
<input type="submit" id="submitButton" name="submitAction" value="Save" />
<input type="submit" id="submitButton" name="submitAction" value="Submit" />
}
You need to specify action at the form level.Not at submit button
<form action="controller/action" method="post">
<input type="submit" id="submitButton" value="Save" />
</form>
It is better to use MVC built in HTML herlper classes like
#using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post))
{
<input type="submit" id="submitButton" value="Save" />
}
I have found out the problem
Basically I had to change this
<input type="submit" id="submitButton" name="submitAction" value="Save" />
<input type="submit" id="submitButton" name="submitAction" value="Submit" />
to this
<input type="submit" id="saveButton" name="submitAction" value="Save" />
<input type="submit" id="saveButton" name="submitAction" value="Submit" />
The submitButton didn't seem to post correctly :)
Related
I am using the tag helper normaly like this:
<a asp-action="ActionName" asp-route-id="#item.id" class="btn btn-sm btn-info mt-2">Edit</a>
or <button asp-action="ActionName" asp-route-id="#item.id class="btn btn-sm btn-info mt-2""></button>
However, when I try to use the tag helper with a composite primary key I am unable to get the link right.
asp-route-id="#item.ID1, #item.ID2" = ActionName/Id1,Id2
asp-route-id="#item.ID1?ID2=#item.ID2" = ActionName/Id1%3FID2%3DId2?
How can I use the asp-route-id and have it putput /Id1?ID2=Id2 or /Id1?{SpecifiedName}=Id2?
I found a workarout for anchor using Html.ActionLink, but not for button
This is a working demo.
<form method="post">
<label>Username:</label>
<input type="text" name="username" class="form-control" />
<label>Password:</label>
<input type="password" name="password" class="form-control" />
<button asp-action="Index" asp-route-id="1" asp-route-id2="2" class=" btn btn-info ">Edit</button>
</form>
View:
After submit the button, the generate url:
https://localhost:5001/Home/Index/1?id2=2
The request payload:
Controller action:
<form action="#Url.Action("PokemonSteps", "PokemonView", new { id = Model.Id, steps = steps })">
<input type="number" name="steps" min="1" max="#Model.userSteps">
<input type="submit">
</form>
This form is a number box from 1 to the amount of steps the user has.
When they submit the number in the box, I want to pass it to #Url.Action as the steps in the steps = steps part.
How do I go about that (sorry for the really stupid question)
<form action="#Url.Action("PokemonSteps", "PokemonView")" method="post">
<input type="hidden" name="id" value="#Model.Id">
<input type="number" name="steps" min="1" max="#Model.userSteps">
<input type="submit" value="Submit">
</form>
Please add Attribute method="post" and share your controller method signature also...
Try doing something like this:
#using (Html.BeginForm("PokemonSteps", "PokemonView", FormMethod.Post)){
#Html.TextBoxFor(m=> m.userSteps)
<input type="submit" value="Submit" />
}
Hope this Helps!!
This question already has answers here:
MVC which submit button has been pressed
(11 answers)
Closed 6 years ago.
In my MVC form starting with #Html.BeginForm I have put two submit buttons:
<div>
<input id="submitButton" type="submit" value="Register" />
<input type="hidden" name="btn" value="register" />
</div>
<div>
<input id="cancelButton" type="submit" value="Cancel" />
<input type="hidden" name="btn" value="cancel" />
</div>
But in my action method, the value that is coming in is still "register" from first button, even if I click the Cancel button.
How can I fix this?
The hidden fields have nothing to do with the buttons, but their values are what you are receiving, regardless of which submit button you click.
Remove the hidden fields, and add the name="btn" attribute to each of the buttons. The value you receive in btn will be the value (and also the exact text) of the button that was pressed.
Example:
<div>
<input id="submitButton" type="submit" name="btn" value="Register" />
</div>
<div>
<input id="cancelButton" type="submit" name="btn" value="Cancel" />
</div>
i have a page with multiple forms like this: (all of them have different names
<form method="post" name="form1">
<input type="text>
<input type="text>
<input type="text>
<input type="text>
<button type="submit" name="button1" class="btn btn-icon btn-primary glyphicons circle_ok"><i></i>Save changes</button>
</form>
This is the code i'm using to check which form is submitted:
if(IsPost && !Request["button1"].IsEmpty()) {
}
the code above only works if i submit the form through an <input type="submit" name="button">
i wanted to know if there's any way to know which form is submitted with a button type=submit (the one that's in the form i posted above)
This is because the value of the a <button> element is not posted. You could add a hidden field to your form:
<input type="hidden" name="formname" value="myform" />
Then check for this in your code:
if(IsPost && Request["formname"] == "myform") {
}
Can you have a different value attached to a submit input than the name it shows.
something like, this is just a test, doesn't actually work:
<input type="submit" name="btn" value="Value to show as button text" value="value to use" />
The format I'm using is cshtml.
Edit:
What I'm trying to accomplish is to have a generated list of objects which each have a button to return a Request["btn"], or similar, to use in C#.
All buttons need the same name, but have to parse the id of the object to the request.
Some code:
#{
if(Request["btn"] == "Value to show as button text")
(
int id = // submit object id
}
<form action="" method="post">
<ul>
<li>
<input type="submit" name="btn" value="Send request" value="id1" />
</li>
<li>
<ul>
<li>
<input type="submit" name="btn" value="Send request" value="id3" />
</li>
</ul>
</li>
<li>
<input type="submit" name="btn" value="Send request" value="id2" />
</li>
</ul>
</form>
change
<input type="submit" name="btn" value="Value to show as button text" value="value to use" />
to
<input id='InputVal' type="submit" name="btn" value="Value to show as button text" data-value="value to use" />
and take the value as
var InputValue=$('#InputVal').attr('data-value');
Yes, use the <button> tag:
<button type="submit" value="value-to-submit">Button Text</button>
you code already show "Value to show as button text" on the button to user, and value that will be passed will be "value to use"..