Issue related to Submit HTTPPOST ActionMethod in MVC - c#

I have asp.net MVC Web application.
i have an input button:
<input type="submit" name="Report" value="To File" id="rptToFile" />
i have another input textbox :
<input type="text" id="txtMemItem1" name="" />
when user hits enter in TextBox i have written code to open a new popup:
but problem is: by hitting enter, it calls HTTPPOST Action Method in Controller.. i dont want to submit it on EnterPress key, but want to open popup..
how can i solve my problem ???
THanks

That is default behavior of the HTML forms Whenever focused on form control and you pressed enter form gets posted to the server.
Alternative solution to this behavior can be write a javascript onkeypress event and check if keyCode is 13 (enter) then return false and open a popup else return true.
HTML
<input type="text" onKeyPress="keyPressed(event)" .../>
Javascript
function keyPressed(event)
{
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13)
{
//open a popup here.
return false;
}
return true;
}
Alternatively if you don't need to post the form ever then you can use something like below too.
<form onsubmit="return false" ></form>

Related

Intercept POST of a form in CefSharp

I am bulding a WinForms application (VS2019, .NET 4.7.2) that wants to use CefSharp as its main UI (Cef 79.1.36+g90301bd+chromium-79.0.3945.130).
I have a simple form loaded from a local html file called form.html, where the relevant section (omitting most of the HTML boilerplate) looks like this:
<form method="POST">
<input type="text" name="Name" placeholder="Name" /><br>
<input type="text" name="Place" placeholder="Place" /><br>
<input type="submit" />
</form>
The form file is loaded via a custom scheme:
settings.RegisterScheme(new CefCustomScheme
{
SchemeName = "folder",
DomainName = "local",
SchemeHandlerFactory = new FolderSchemeHandlerFactory(
rootFolder: $#"{Application.StartupPath}\wwwroot",
hostName: "local",
defaultPage: "index.html")
});
The code that actually loads it is in Form1.cs and looks like this:
browser.Load("folder://local/form.html");
The form loads fine and is displayed as expected.
Now, when the user presses the Submit button, I want to intercept the submission and handle the submitted data (the values entered for Name and Place in this case) in my C# code. I don't want the form to actually submit to any web server - I just need to be able to get to the form data and deal with it in my C#/WinForms code.
I am not sure how to do that. Right now, after the Submit button is clicked, the form gets cleared (presumably reloaded from the same url), but I don't know where the submitted data went. Would appreciate any clues.

Cancel button does not get called when ModelState is not valid

I had these two buttons:
<button id="submit" name="button" value="register" class="btn btn-primary">Submit</button>
<button id="cancel" name="button" value="cancel" class="btn btn-secondary">Cancel</button>
In a page that has some text fields with some validation on them for example
#Html.TextBoxFor(m => m.EmailRequest, new { #class = "form-control", type = "email" })
#Html.ValidationMessageFor(m => m.EmailRequest)
And then to know if cancel button was cliked to redirect them to some other page, in controller I had it like this:
[HttpPost]
public ActionResult ForgotPassword(string button, ForgotPasswordViewModel fpModel)
{
// cancel button, go back to LoginPage
if (!string.IsNullOrEmpty(button) && button == "cancel")
{
return RedirectToAction("Login");
}
else
{
// blah
}
}
It works if they fill out good valid values in the text boxes and then they click on cancel BUT it does not even get called when there are validation errors on the text boxes and then click cancel. So it won't redirect them either.
So what should i do ?
It sounds as if you are trying to POST the form back to the server in both scenarios (cancel or submit).
It doesn't make much sense to POST a form back to a server if there are validation issues. If you do this, you then rely on server side validation checking and make sure you re route to Login. You should always have server side, but it is obviously best to not just rely on it and to have both server side and client side validation.
An alternative solution would be to replace the cancel button with a link.
E.g.
#Html.ActionLink("Cancel", "Login")
This is also quicker in terms of performance, because it will simply redirect the user to the login page. Instead of posting back to the server and then re routing.

ASP.Net Postback Broke After Changing Form Attribute

I have an ASP.Net form which looks like the following when rendered (many lines of markup removed for brevity):
<form onsubmit="document.body.style.cursor='wait';showLoader();" action="mypage.aspx">
<!-- form elements and controls -->
<input type="submit" />
</form>
In mypage.aspx.cs's Page_Load I am doing:
if(Page.IsPostBack)
{
Foo();
} else {
Bar();
}
Quux();
Without modifying the HTML page using the Chrome Dev Tools, if I submit the form it executes Bar(). But If I removed or replaced the onsubmit attribute in the form with my own, ASP.Net thinks that the page is doing a post back and executes Foo().
I find this behavior weird because the inline JS code in onsubmit does not change anything related to the form submission. Its function is only to add a loading GIF while the form is reloading. Could there be something I am missing here?
Instead of adding a submit button, try adding a regular button, and call your gif functionality from it:
<input type="button" onclick="document.body.style.cursor='wait';showLoader();" value="Click Me" />
This will not post anything, which is what you want. <input type='submit'> will always cause a postback, unless you cancel it with JavaScript.
JSFiddle: http://jsfiddle.net/0gd8t7ew/

Making a button element submit

Using ASP.NET, how do I make this button tag submit my ASP.NET form when clicked:
<button>Submit</button>
I'd like it to do a post back just like a regular asp.net server control button would work. I'd prefer a jquery way to do it if possible.
$('button').on('click', function(){
$(this).closest('form').submit();
});
On button click, it will find the closest form (which will be the parent) and submit it by passing the form to the action where you can access the values through your defined method; either get or post.
I think this is what you're asking.
You can either use:
<input type="Submit" />
Or using jQuery you can use:
$(function() {
$('button').on('click', function() {
$('form').submit();
});
});
you use javascript when you have links, or divs, or other elements that can not do post back.
In your case the <button>Submit</button> in html5 renders a submit button that if you click it, you just submit the form and that all you need.
The extra asp.net controls have some more functionality and communication with the code behind, but for the submit of the form, any submit button ether that one, ether the classic <input type="submit" value="Submit"> can do what you ask as they are.

Clear TextBox in Login Control when onclick

i have login control and textbox inside , i need to write UserName in textbox to show to user and i need when click inside textbox to clear this textbox and allow user to enter text
Since you asked for javascript code
<input type="text" value="your name" name="usrname" onfocus="if(this.value==this.defaultValue) this.value='';" onblur="if(this.value=='') this.value=this.defaultValue;"/>
use the placeholder html5 attribute
if using jQuery is acceptable:
jQuery("#myTextBox").focus( function(){
$(this).val("");
} );
or in javascript
<input type="text" value="A new value" onfocus="javascript: if(this.value == 'A new value'){ this.value = ''; }" onblur="javascript: if(this.value==''){this.value='A new value';}" />
Last year I wrote a jQuery plugin for exact that problem. It's called "toggletextfieldvalue" check it out http://www.naden.de/blog/toggle-textfield-value-jquery-plugin
Note: If you are using HTML5, you can rely on the new placeholder attribute.

Categories

Resources