Search code examples
asp.net-mvc-3entereventtrigger

MVC3 input button triggered by Enter button


I have an ASP.NET MVC 3 C#.NET web application and I want the Enter button to trigger a particular Save button on my form. How would I do that?


Solution

  • What I would do is utilize JavaScript for this one. Handle the onkeypress even of the form. Like so:

    <form onkeypress="yourKeyPressFunction(event)">
    ...
    </form>
    

    Then your yourKeyPressFunction() would look something like this:

    function yourKeyPressFunction(e)
    {
        if (e.keyCode == 13)
        {
            // submit or do what you'd like when you hit enter
        }
    
        // ... so on and so forth
    }