When Ajax.BeginForm() may ignore submit on enter press?

Question is quite straightforward:

When Ajax.BeginForm() may ignore form submit on enter press ?

Solution:

A form MAY BE submitted without an <input type="submit"> (through) enter key press if:

There is only one html element inside the form of type <input type="text"> (with textarea doesn't work).

A form MAY NOT BE submitted via enter key press if:

There is more than one html element inside the form of type <input type="text">, and no <input type="submit">

Answers


First of all make sure there is a submit button inside your form. In case you do not want to have any submit button on the form (although it would be a bit strange, but ok), you could add a submit button and hide it like this:

<div class="hidden-submit"><input type="submit" tabindex="-1"/></div>

And then in the CSS file:

.hidden-submit {
    border: 0 none;
    height: 0;
    width: 0;
    padding: 0;
    margin: 0;
    overflow: hidden;
}

Another solution is to add the following in Javascript:

$(window).keypress(function (event) { 
    if (event.which == 13) { 
       document.forms[0].submit(); 
       // assuming that you have just one form on the whole page
    } 
});

It gives me the solution for my Problem :-

Problem :- I have only single textbox, single textArea, other html input fields and one button of type=button in my view Form of MVC appplication. when i was pressing enter key in textbox it was firing postback and that was causing issue for me.

Solution :- From above christian's solution, i came to know that when there is a single textbox used with html.Beginform tag in mvc the enter key press event fires the submit button event. Probably this is not the answer that should i post here, but i glad to found solution from this post, that's why i am posting my answer here.


Need Your Help

HTML5 audio player drag and drop

javascript html5 drag-and-drop html5-audio

I'm implementing audio player in html5, which has drag-drop option for load and play mp3 files, but I have problem with plaing the file.

C# - SMTP - GoDaddy - Send Email

c# email smtp

What am I doing wrong? Im trying to send a email using c# with GoDaddy webhost.