A Bogus
A Bogus

Reputation: 3930

MVC Html.BeginForm capture key press event

I have an MVC application, and I am using Html.BeginForm to make forms. There are several forms on the page, each representing a tab in a row of tabs. I need to detect the "Enter" key being pressed on one of the tabs. Anyone know how to do this?

Code example -

<div>
@using (Html.BeginForm("MyAction", "MyController", FormMethod.Post, new { id = "MyForm1" }))
{
    <div id="divId">
    </div>
}
</div>

Thanks!

Upvotes: 0

Views: 3165

Answers (1)

Joshua Espana
Joshua Espana

Reputation: 114

@using (Html.BeginForm("Create", "ApplicationSchedule", FormMethod.Post, 
        new { onkeydown = "return event.keyCode!=13" }))

You are able to add event listeners to the form using the htmlAttributes object parameter as I have done using the new { onkeydown = "return event.keyCode!=13" } section in the code. In my case I am ignoring the enter key when it is pressed in the form using the onkeydown listener. This is easily replaceable using your own code such as onkeydown = "yourJavascriptFunction()" to achive whatever it is you need to achieve when the enter key is pressed in the form. For more info on the parameters accepted for the Html.BeginForm(), see MSDN

Upvotes: 2

Related Questions