FormCollection missing submit button values [duplicate] - c#

This question already has answers here:
How do you handle multiple submit buttons in ASP.NET MVC Framework?
(35 answers)
Closed 9 years ago.
This code was working at one point. I'm not sure what broke it, but basically on postback I'm looking for the existence of a submit button key in the FormsCollection. Depending on the existence, I perform different operations. The custom attribute to handle this is getting hit. When I step through it and look at the FormsCollection, I can see all key/values from the form except for the submit button which submitted the form.
Looking at the submission via Fiddler2, it doesn't even look like the submit button is being sent to the server with the rest of the values. If I put in a hidden field with the same name I am looking for (and hard coded value) I can successfully hit the "if exists" logic branch because the hidden field's value is sent to the server.
Here's my form:
#using (Html.BeginForm("Respond", "AccountInvitations", new {id = Model.Invitation.InvitationCode, ReturnUrl = Request.QueryString["ReturnUrl"] ?? string.Empty}, FormMethod.Post, new {#class = "invitation-details-form"}))
{
<div class="modal-body">
<div class="status-message info">
<i></i>
You have been invited to join <strong>#Model.Invitation.AccountName</strong>.
Create your profile below to join this account.
</div>
#Html.AntiForgeryToken()
#Html.ServerValidationSummary()
#Html.EditorFor(model => model.InvitationResponse, "InvitationResponseDto", "")
</div>
<div class="modal-footer">
<div class="button-container clearfix">
<input type="submit" name="accept" value="Accept Invitation" class="btn btn-secondary" />
<input type="submit" name="decline" value="Decline Invitation" class="btn btn-link cancel" />
</div>
</div>
}
Neither "accept" nor "decline" shows up in the FormsCollection when I press on either (the form is submitted and all fields from the editor template show, but not the submit inputs).
I do not want to handle this with javascript setting a hidden field value.

Wow, it's been a long couple nights coding. This post led me to the answer. A couple weeks back a script was added to prevent double clicking on some form entries. The form disabled the inputs immediately upon form submission. A simple setTimeout() to delay the disabling did the trick.

OR you can using
<div class="modal-footer">
<div class="button-container clearfix">
#using (Html.BeginForm("Your_ActionResult", "Your_controller"}, FormMethod.Get/Post))
{
#Html.AntiForgeryToken()
<input type="submit" name="accept" value="Accept Invitation" class="btn btn-secondary" />
}
#using (Html.BeginForm("Your_ActionResult", "Your_controller"}, FormMethod.Get/Post))
{
#Html.AntiForgeryToken()
<input type="submit" name="decline" value="Decline Invitation" class="btn btn-link cancel" />
}
</div>
</div>
you must specify the submit button that call the ActionResul method in your Controller is

Related

The text displayed on my HTML button is including more than it should

Here is a reference Image
For a little bit more context I am operating in a C# ASP.NET MVC environment and I am trying to pass a value to a form via button click. For my other two values that are being assigned on the form I am using #Html.EditorFor() and #Html.RadioButtonFor() for a text box and radio button selection respectively. However since there is no #Html.ButtonFor() I am instead trying to assign the value to the form with #Html.HiddenFor() inside of an onclick. This works in terms of the functionality I want from it, but there is a weird visual issue where the symbols following the onclick are also used in the text displayed on the button, so while the button should display the text:
Save Notification
It actually displays
"> Save Notification
Here is the relevant code (with specifically the button markup at the bottom where the issue is taking place). I feel like I am missing something very obvious here and I am very new to HTML and Kendo so I am really lost as to why this is happening.
<div class="row">
<div class="col-sm-6 ref-bottom-margin">
#Html.EditorFor(model => model.SystemStatus.UniversalNotification.SystemNotification, new { htmlAttributes = new { #class = "form-control" } })
</div>
<div class="col-sm-4">
#Html.RadioButtonFor(model => model.SystemStatus.UniversalNotification.BannerSeverity, "Alert")
Alert
#Html.RadioButtonFor(model => model.SystemStatus.UniversalNotification.BannerSeverity, "Warning")
Warning
#Html.RadioButtonFor(model => model.SystemStatus.UniversalNotification.BannerSeverity, "Notification")
Notification
</div>
<div class="col-sm-2">
<button class="btn btn-primary" type="submit" value="Universal" onclick="#Html.HiddenFor(model => model.SystemStatus.UniversalNotification.BannerScope)"> Save Notification </button>
</div>
</div>
#Html.HiddenFor will generate " inside onclick attribute, and causing wrong text display
Rendered source code of the button:
<button class="btn btn-primary" type="submit" value="Universal" onclick="<input id="SystemStatus_UniversalNotification_BannerScope" name="SystemStatus.UniversalNotification.BannerScope" type="hidden" value="" />"> Save Notification </button>
Instead of using #Html.HiddenFor, you can get the value from model using #(Model.fieldname). And maybe you will want to pass the value directly into JavaScript function, like below
onclick="someFunction('#(Model.SystemStatus.UniversalNotification.BannerScope)')"

How to pass an argument(id) from page view to page model

let me explain a problem first.
I've got a page.
Page is basket representation with multiple records (items).
Every item has a delete button. What I want to achieve is to delete item from that basket (actually from database). But first I need to pass item id to page-model post method.
Right now that is what i got at Page view.(cshtml, dont really sure how to call it)
<form method="post">
<div class="form-group">
<input type="submit" class="btn btn-danger" value="Delete" asp-page-handler="Delete"/>
</div>
</form>
And here is a page model.
public IActionResult OnPostDelete(int itemId)
{
memberData.DeleteFromBasket(itemId);
return RedirectToPage("./Basket");
}
How can i pass item id to the method?
You need to add a route parameter to the button that matched the parameter name. For example:
<input type="submit" class="btn btn-danger" value="Delete"
asp-page-handler="Delete"
asp-route-itemId="#model.ItemId" />

ASP.net Webforms HTML5 "required" not working

I am quite new to ASP.NET, I am trying to use the HTML5 required attribute on a textbox.
<form action="/../.." method="post" style="margin-bottom: 20px;" id="testform">
<input type="text" name="Username" required="required">
<input type="submit" class="btn btn-primary" value="Sign In" name="Submit"/>
</form>
When i submit my form without filling the textbox with data, it doesnt show any error message:
When I inspect my form, i see a class has been added by required, but no error message shows:
The same code works fine in JSFiddle, Check it out here

Can't get input type="text" value in action on form submit?

In a Razor view I have input type="text", a hidden field and a button. I can access hidden field from Form collection but its weird I cant access input type="text" value inside my action. I am not sure if my understanding is correct or not but I was thinking as all fields inside form should be available inside action.
Below is my code please:
#using (Html.BeginForm())
{
<div style="margin-top: 40px;">
<input id="txtDateFrom" class="span2" size="16" value="#Model.StartDate.ToString("dd/MM/yyyy") " readonly="readonly" type="text">
#Html.Hidden("currencyCode", (object)ViewBag.currencyCode)
</div>
<button onclick="#Url.Action("ExchangeRateDetails", "ExchangeRate")" class="btn btn-lg span2 ARML50px">
}
I highly appreciate your time, guidance and help.
The reason your hidden input works is that you render this with help from the Html helper #Html.Hidden. This helper render the input field with the name attribute.
Your <input type="text"> is missing the name attribute. So try writing like this:
<input id="txtDateFrom" name="txtDateFrom" class="span2" size="16" value="#Model.StartDate.ToString("dd/MM/yyyy") " readonly="readonly" type="text" />
The name="txtDateFrom" will make the value appear in your FormCollection.
Try this way
You don't need onclick="#Url.Action("ExchangeRateDetails", "ExchangeRate")" to the button
Change your button for below way
<button type="submit" class="btn btn-lg span2 ARML50px">
And Now your controller can get the input text
[HttpPost]
Public ActionResult ExchangeRateDetails(YourmodelClass xxx)
{
string dates=Model.StartDate;
}
This website have lot and lot of answers for how to send a model values from view to controllers by see the Related discussion on this page right corner .

HTML form submit button doesn't sent data form

I use DNN and C#. I've got a form with a lot of input and select HTML tag. At the end of form there is a . When I click on it, the page is reload but when I try catch data of form (with Request.Form['formName'] or Request['form']) I've got only empty field (and if I check the value of IsPostback, it's always false). But in the form tag there is value.
FILE View.ascx
<div class="row">
<div class="col-sm-2"> </div>
<div class="col-sm-4">
<label for="formCodiceCanile">Codice Canile</label>
</div>
<div class="col-sm-4">
<input type="text" name="formCodiceCanile" ID="formCodiceCanile" value="<%=InfoScheda.CodiceCanile %>" />
</div>
<div class="col-sm-2"> </div>
</div>
OTHER FIELD (such as input text, select and radio button)
<input type="submit" name="formRegistraScheda" id="formRegistraScheda" value="Registra scheda" />
I know that I can use (and if I use it, there isn't problem because catch the value in the asp:Textbox it's a joke) but in this case I can't use them.
How can I resolve this problem?
PROBLEM SOLVED: the button doesn't do the POST because I use a field's form in the URL to observe the page status. Now I remove this control and I use the IsPostback native variable of C# and the form work!

Categories