In ASP.NET, you were able to set the default button for a form by simply specifying the name of the button you wanted to use when a user clicked on the Enter button. In ASP.NET MVC, we may still want to be able to configure which button we want to use by default. Regardless of need, this exercise will hopefully be a good introduction to using jQuery with your ASP.NET MVC form.
HomeController
. We're going to add an additional action called Article
that takes two parameters—the form data in the form of an Article
and a string to capture the button that was pressed. The action that captures the Article
will allow only posts to it, so we'll decorate the action with an HttpPostAttribute
.Controllers/HomeController.cs:
[HttpPost]
public ActionResult Article(Article article, string btnSubmit)
{
return View(article);
}
You can have action overloads, just like you can have method overloads, because an action is a method. Compiling isn't the issue, the issue is the application's interpretation of the values passed into the routing dictionary. If you're going to overload your actions, make sure that they're unique enough to not leave any room for interpretation. Avoid multiple action overloads that take the same amount of nullable parameters. Making use of attributes such as HttpPost
will also help avoid ambiguity.
btnSubmit
. This allows us to capture either button when clicked in the controller. The form can be found in Article.aspx
or _articleFormFields.ascx
, depending on which recipe you have just finished.Views/Home/Article.aspx:
<input type="submit" name="btnSubmit" id="submitButton" value="Submit" />
<input type="submit" name="btnSubmit" id="cancelButton" value="Cancel" />
Views/Home/Article.aspx:
<% using (Html.BeginForm("Article", "Home", FormMethod.Post, new Dictionary<string, object> { { "data-default", "submitButton" }, { "id", "form1" } })) { %>
Site.Master
file in the Views/Shared
directory. Make some whitespace under the css
link, then expand the Scripts
directory and drag over the jquery-1.4.1.js
file.Views/Shared/Site.Master: <head runat="server"> ... <script src="/Scripts/jquery-1.4.1.js" type="text/javascript"></script> </head>
Article.aspx
file. Directly above the content of the Article
page, (above the default <h2>
tags) we will add a new <script>
block. Inside this script
block, we'll add the script, which will be responsible for capturing the Enter key (character 13). Once captured, we'll trigger the click
event of the button specified in the data-default
attribute.Views/Home/Article.aspx:
<script type="text/javascript">
var buttonKeys = { enterKey: 13 };
$(function () {
$("#form1").keypress(function (evt) {
if (evt.which == buttonKeys.enterKey) {
evt.preventDefault();
var defaultButtonId = $(this).attr("data-default");
if (defaultButtonId != null)
$("#" + defaultButtonId).click();
}
});
});
</script>
Home/Article
and view the form. Then put your cursor in any of the fields and hit Enter. This should submit your form. If you set a break point in your post-handling action, you should be able to see that the submitButton
is the button that was used.HTML5 data attributes provide an ideal way to add additional information to your markup. When the page is loaded, our jQuery script overrides the keypress
event of the form with a custom function. If the Enter key is pressed, we trigger the click
event of the specified default button. If you have plain old text areas in your form, you'll need some refinement to avoid creating a new line and inadvertently submitting the form at the same time.