Having two submit buttons and knowing which one is clicked [duplicate] - c#

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>

Related

How to get RadioButton`s label from html MVC

I`ve got three radio buttons in my form. Every rbutton has own label as below
<form method="post">
<input type="radio" name="radio" "onclick="removeField()">
<label>Text1</label>
<br>
<input type="radio" name="radio" onclick="removeField()">
<label>Text2</label>
<br />
<input type="radio" name="radio" onclick="addField()">
<b>Another</b>
<br />
How I can get in my c# code label of radiobutton which is on?
You could just make the radio button's value be the same text as the label. When you post the form it should give you a key/value pair of 'radio=Text1' (or whatever.
<form method="post">
<input type="radio" name="radio" value="Text1" "onclick="removeField()">
<label>Text1</label>
<br>
<input type="radio" name="radio" value="Text2" onclick="removeField()">
<label>Text2</label>
<br />
<input type="radio" name="radio" value="Another" onclick="addField()">
<b>Another</b>
...
A few additional notes:
If label wraps the radio button, it becomes clickable and will toggle the radio button. Make sure to put the 'onclick' on the label tag if you do that.
The name 'radio' for a form field is not illegal, but I would suggest it's complicated. That radio button represents a property of your viewmodel (or of something) so I would suggest calling it something like 'SelectedText' or whatever.

how to create two buttons within form and have only one submit form

I am trying to create a "next" button and a "back" button in my form. I want the "next" button to validate and submit the form. And I want the "back" button to simply go back to the "cart" page. But the "back" button keeps validating and trying to submit the form.
Here is my code:
<div class="buttons">
<button class = "button" id = "back">Back</button>
<input type="submit" value="Next" class="button" />
</div>
The reason I need the back link to be a button is so that it will look the same as the "next" button. Any ideas how I can get this working correctly?
A <button> element always submits the form. The same goes for a <input type="submit" /> element.
But a <input type="button" /> will not submit the form. That's what you want.
<div class="buttons">
<input type="button" class="button" id="back" onclick="window.location = '#Url.Action("Index", "Cart")';">Back</a>
<input type="submit" value="Next" class="button" />
</div>
Edit I'm not sure if you can put that inside an <a> element though. I reworked my example to use click events rather than a link. If it is valid to put inside a <a> element (can anyone confirm?), you can do it like that as well.

MVC form input button with name doesn't get submited

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 :)

Check if button has been submitted

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") {
}

input submit with two types of value

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"..

Categories

Resources