Null Reference
Null Reference

Reputation: 11340

Handling form submit event

I have the following form:

@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl, @class = "form-vertical login-form", id = "loginform" }))

                    <button type="submit" class="btn green pull-right">
                        Login <i class="m-icon-swapright m-icon-white"></i>
                    </button>

}

And this the javascript handling the event function

$(document).ready(function () {

            $("#loginform").submit(function () {
                alert('Handler for .submit() called.');
                return false;
            });
}

However this doesn't work at all.

the alert is never triggered

Upvotes: 0

Views: 1128

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038830

That's normal, you never assigned your form an id or a class. Look at the generated HTML in your browser to understand what I mean.

You are using a completely wrong overload of the BeginForm helper.

Try this instead:

@using (Html.BeginForm(null, null, new { returnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-vertical login-form", id = "loginform" }))
{
    <button type="submit" class="btn green pull-right">
        Login <i class="m-icon-swapright m-icon-white"></i>
    </button>
}

Now please read about the different BeginForm overloads on MSDN and compare yours with mine.

As you can see there's a difference between the routeValues parameter and the htmlAttributes parameter.

Upvotes: 3

Related Questions