Monday, 21 July 2014

Setting default submit button in MVC3 razor using jQuery

In Asp.net MVC, sometimes we required to post the form on Enter key press. Asp.net MVC has no default button property like Asp.net. However, we can achieve this functionality by using jQuery in MVC.

Set Form DefaultButton Property using jQuery


<script type="text/javascript">
    $(document).ready(function () {
        $("#MyForm").keypress(function (e) {
            kCode = e.keyCode || e.charCode //for cross browser
            if (kCode == 13) {
                var defaultbtn = $(this).attr("DefaultButton");
                $("#" + defaultbtn).click();
                return false;
            }
        });
    });
</script>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { DefaultButton = "SubmitButton", id = "MyForm" }))
{
    @Html.TextBox("txtname")
    <span>Please Enter value and then press Enter Key</span><br />
    <input type="submit" name="btnSubmit" id="SubmitButton" value="Submit" />

}

Summary

In this article I try to explain the default submit behavior of form in MVC. I hope you will refer this article for your need. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

1 comment:

  1. This web-site is actually a walk-through it really is the info you wished with this and didn’t know who must. Glimpse here, and you’ll definitely discover it. razorwang

    ReplyDelete

Topics

ADO .Net (2) Ajax (1) Angular Js (17) Angular2 (24) ASP .Net (14) Azure (1) Breeze.js (1) C# (49) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DI (3) Dotnet (21) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (9) Lamda (3) Linq (11) Mongodb (1) MVC (48) NodeJS (7) RDLC (1) Report (1) Sql Server (29) SSIS (3) SSRS (2) UI (1) WCF (12) Web Api (10) Web Service (1) XMl (1)