Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8967621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:17:06+00:00 2026-06-15T17:17:06+00:00

Working on my first MVC 4 app, and I’m having a problem with my

  • 0

Working on my first MVC 4 app, and I’m having a problem with my login screen. It is an intranet app for a company with common PCs on a manufacturing floor, so they need to be able to log in to the app on an individual basis. (The site is also in jQuery mobile and has a Javascript touch keyboard, but I don’t think they need be involved in this question)

The problem is that if someone fails to log in, the Javascript code that sets it so that enter key presses in the Username field send the focus to the Password field stops working.

I have a hack that works, (using setTimeout, 100) but it makes me feel icky and dirty.

Here’s my LogOn form:

@ModelType FAToolAdmin.LogOnModel

@Code
    ViewData("Title") = "Log In"
End Code


@Using Html.BeginForm()
    @<div class="content-primary">

        <div data-role="header" data-position="fixed" data-theme="b">
            <h2>Account Info</h2>
        </div>
            
        <fieldset>
            <div class="editor-label">
                @Html.LabelFor(Function(m) m.UserName)
            </div>

            <div class="editor-field">
                @Html.EditorFor(Function(m) m.UserName)               
            </div>

            <div class="editor-label">
                @Html.LabelFor(Function(m) m.Password)
            </div>

            <div class="editor-field">
                @Html.EditorFor(Function(m) m.Password)
            </div>

            <p>
                <input type="submit" value="Log In" class="notfocus"/>
            </p>

            @* This Html.ValidationSummary will show a ul element containing any login errors that were returned.  
                The call here has been edited to send an empty string, because sending 2 lines of text regarding 
                a login failure is a little excessive.  You failed to login.  Type everything again and move on 
                with your day.*@

            @Html.ValidationSummary(True, "")

        </fieldset>
    </div>

End Using

<script type="text/javascript">

    //This is the script that adds the keyboards to the screen.

    $(function () {
        //setTimeout(function () {

            //This is the script that makes it so that when the enter button is pressed on the screen that the 
            //focus is changed to the password box (rather than having the form be submitted).

            // set the focus to the UserName field and select all contents
            $('#UserName').focus().select();
            // bind the keypress event on the UserName field to this little piece of code here
            var $inp = $('#UserName');
            $inp.bind('keydown', function (e) {
                var key = e.which;
                // if this is keycode 13 (enter), then prevent the default form behavior and change focus to the Password box.
                if (key == 13) {
                    e.preventDefault();
                    // find the Password input box and set the focus to it.
                    $('#Password').focus();
                }
            });
        //}, 100);
    });

</script>

and here’s my controller:

Imports System.Diagnostics.CodeAnalysis
Imports System.Security.Principal
Imports System.Web.Routing

Public Class AccountController
    Inherits System.Web.Mvc.Controller

    ' GET: /Account/LogOn
    <AllowAnonymous()>
    Public Function LogOn() As ActionResult
        Dim model As New LogOnModel
        Return View(model)
    End Function

    '
    ' POST: /Account/LogOn
    <AllowAnonymous()>
    <HttpPost()>
    Public Function LogOn(ByVal model As LogOnModel, ByVal returnUrl As String) As ActionResult
        Try
            If ModelState.IsValid Then

                If Membership.ValidateUser(model.UserName, model.Password) Then

                    FormsAuthentication.SetAuthCookie(model.UserName, False)
                    If Url.IsLocalUrl(returnUrl) AndAlso returnUrl.Length > 1 AndAlso returnUrl.StartsWith("/") _
                       AndAlso Not returnUrl.StartsWith("//") AndAlso Not returnUrl.StartsWith("/\\") Then
                        Return Redirect(returnUrl)
                    Else
                        Return RedirectToAction("Index", "Home")
                    End If
                Else
                    ModelState.AddModelError("", "The user name or password provided is incorrect.")
                End If
            Else
                ' if you just click on login without ever having entered anything into the username and password fields, you'll get this message.
                ModelState.AddModelError("", "Please enter a valid user name and password.")
            End If

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

        ' If we got this far, something failed, redisplay form
        Return View(model)

    End Function

    '
    ' GET: /Account/LogOff
    <AllowAnonymous()>
    Public Function LogOff() As ActionResult
        FormsAuthentication.SignOut()
        Session.Abandon()
        Return (RedirectToAction("Index", "Home"))
    End Function

End Class

and model:

Public Class LogOnModel

    <Required()> _
    <Display(Name:="SomeCompany Username")> _
    Public Property UserName() As String

    <Required()> _
    <DataType(DataType.Password)> _
    <Display(Name:="Password")> _
    Public Property Password() As String

End Class

So my question is, how do I get it set up so that someone who types in a bad username (and hence triggers a ModelState.AddModelError and Return View(model)) is sent back to a version of the page where the enter press script has been loaded?

I feel like I’m missing something fundamental. Appreciate any help someone can give. Otherwise, setTimeout it is.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:17:07+00:00Added an answer on June 15, 2026 at 5:17 pm

    I think the jQueryMobile might be your problem – jqm uses ajax by default to submit all forms, so you’re not getting a full postback. I ran into a similar problem and that was the root cause.

    Try using an overload for Html.BeginForm and add new {data_ajax="false"} to the HttpAttributes

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am working with my first storyboard application and am having a problem. When I
I am working on my first ASP.NET MVC 4 app. The client is deploying
I'm creating my first ASP.NET MVC 3 website for my company's intranet. It's a
I'm working through the Building an MVC 3 App with Code First and Entity
I am working on a mvc3.0 app using EF code first and mvc scaffolding.
I'm working on my first ASP.NET MVC 3 app and I've got a page
I'm new to web development and working on my first ASP.NET MVC 3 app.
I'm working on my first MVC 4 app, following the MVC First Web API
I working on my first ASP.net MVC project and and i got some problems
I'm working on a project in MVC 3 (first time using!). One question I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.