Check if button has been submitted - c#

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

Related

Clicking 2 submit buttons in the same page

I have a project in .Net Core with Razor Pages and I have the form bellow that contains some input fields and 2 submit buttons.
<form class="form-style" method="post" id="createForm">
<div class="form-group button-position col-md4">
<input type="submit" id="placeRequest" name="placeRequest" value="Place Request" class="btn btn-primary" />
</div>
<div class="form-group button-position col-md4">
<input type="submit" value="GetHour" asp-page-handler="Hour" class="btn btn-primary btn-style" formnovalidate />
</div>
</form>
I need to be able to click the first submit button (GetHour) and then click the second one (Placer Request)
In my Razor Page model I have
public async Task<IActionResult> OnPost()
{
//code that will be executed when the Place Request button is clicked
}
public async Task OnPostGetHour()
{
////code that will be executed when the Get Hour button is clicked
}
The problem is that I am not allowed to make the 2 submits. If I only do one of them, it works but the second one does nothing
Is there any way I could make both of the submits?
This is because once you click the first submit button, The page does a redirect and loads again.
This is the nature of form submission. If you want to click both the buttons. Change the button type from "submit" to "button".
Then create 2 click handlers and handle the form submitting using Ajax
<form class="form-style" method="post" id="createForm">
<div class="form-group button-position col-md4">
<input onclick="onBtn1Click()" type="button" id="placeRequest" name="placeRequest" value="Place Request" class="btn btn-primary" />
</div>
<div class="form-group button-position col-md4">
<input onclick="onBtn2Click()" type="button" value="GetHour" asp-page-handler="Hour" class="btn btn-primary btn-style" formnovalidate />
</div>
</form>
Then write 2 JavaScript functions
function onBtn1Click()
{
//Submit code via Ajax here
}
function onBtn2Click()
{
//Submit code via Ajax here
}

MVC ASP.NET Url.Action in form action, passing it form data

<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!!

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

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>

MVC Form Action Automatically Changed by Areas

In my main layout (root), I added search tools (a textbox and a button) to find the products.
_Layout.cs
<form action="#Url.Action("SearchProduct", "Product")" id="frmSearchProduct" method="get" class="form-inline text-right">
<input type="text" name="ProductName" placeholder="Enter Product Name" class="form-control" />
<button type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-search"></i></button>
</form>
The search function works properly, but if I opened page in Areas and clicked the button, it doesn't work. The form action (url) is changed depend on the Areas.
http://localhost:49458/Error/NotFound?aspxerrorpath=/Workflow/Product/SearchProduct
There is no ProductController in Workflow Areas, so that it generates the error. How to solve this?
try with
#Url.Action("SearchProduct", "Product", new { area = string.Empty })

Using twitter bootstrap radio-button as a html.radiobutton replacement

I have a few radio buttons
<p>#Html.RadioButton("selected", "img")img
#Html.RadioButton("selected", "input")input
#Html.RadioButton("selected", "script")script
#Html.RadioButton("selected", "link")link
#Html.RadioButton("selected", "form")form</p>
and I want to use twitter bootstrap's btn-group, buttons-radio as a style as a replacement for these radio buttons, the code i attempted to add was
<div class="btn-group" data-toggle="buttons-radio">
<button type="button" class="btn" name="selected" value="a">a</button>
<button type="button" class="btn" name="selected" value="img">img</button>
<button type="button" class="btn" name="selected" value="input">input</button>
<button type="button" class="btn" name="selected" value="script">script</button>
<button type="button" class="btn" name="selected" value="link">link</button>
<button type="button" class="btn" name="selected" value="form">form</button>
</div>
in my controller i'm using the I'm using the FormCollection as a way of getting the value from the checked radiobutton, but i would like to use the bootstrap css instead of having the plain #Html.radiobutton.
string selectedTechnology = form["selected"];
List.UrlType = selectedTechnology;
I have a feeling that this is not the correct way to do it, since the helper probably works with the formcollection and it has no idea what the bootstrap css buttons are but if there is a way to implement it, that'd be great.
UPDATE:
So... here is the solution I was looking for
radio buttons using bootstrap css
<div id="radios" class="btn-group view-opt-btn-group" data-toggle="buttons-radio">
<button type="button" class="btn" name="option-a" value="a">a</button>
<button type="button" class="btn" name="option-img" value="img">img</button>
<button type="button" class="btn" name="option-script" value="script">script</button>
<button type="button" class="btn" name="option-link" value="link">link</button>
<button type="button" class="btn" name="option-form" value="form">form</button>
<input type="hidden" name="option" value="0" />
</div>
i created a jscript click function
<script type="text/jscript">
$("body").find("#radios").children().each(function () {
$(this).bind('click', function () {
$("input[name=option]").val(this.value);
});
});
</script>
inside my controller I used the FormCollection to add the selected button to my model
string selectedTechnology = form["option"];
whiteList.UrlType = selectedTechnology;
hopefully this helps.
I haven't actually used the twitter bootstrap (only read up on it), and .NET isn't my strong point, but:
1) You need to use input with type="radio" to make a radio button, not the button tag.
2) The twitter bootstrap provides the .radio and .inline classes to be applied to the parent of the input element, like a label:
<label class="radio">
<input type="radio" name="selected" value="..." />
Text to come after the radio button
</label>
3) #Html.RadioButton takes a third argument, an object, for specifying attributes:
#Html.RadioButton("selected", "form", new { class = "class" });
Hope that helps.
So... here is the solution I was looking for
radio buttons using bootstrap css
<div id="radios" class="btn-group view-opt-btn-group" data-toggle="buttons-radio">
<button type="button" class="btn" name="option-a" value="a">a</button>
<button type="button" class="btn" name="option-img" value="img">img</button>
<button type="button" class="btn" name="option-script" value="script">script</button>
<button type="button" class="btn" name="option-link" value="link">link</button>
<button type="button" class="btn" name="option-form" value="form">form</button>
<input type="hidden" name="option" value="0" />
</div>
i created a jscript click function
<script type="text/jscript">
$("body").find("#radios").children().each(function () {
$(this).bind('click', function () {
$("input[name=option]").val(this.value);
});
});
</script>
inside my controller I used the FormCollection to add the selected button to my model
string selectedTechnology = form["option"];
whiteList.UrlType = selectedTechnology;
hopefully this helps.

Categories

Resources