locked
Unobtrusive jQuery and AJAX RRS feed

  • Question

  • User397057121 posted

    Hello!
    I have this page:

    @using System.Web.Mvc.Html
    @using System.Web.Optimization
    @model Lab5.EPAM.WebUI.Models.RegisterUserViewModel
    
    @{
        ViewBag.Title = "Register";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>Register</h2>
    
    
    @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { id = "form" }))
    {
        @Html.AntiForgeryToken()
    
        <div class="form-horizontal">
            <hr />
            @Html.ValidationSummary(true)
    
            <div class="form-group">
                @Html.LabelFor(model => model.UserName, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.UserName)
                    @Html.ValidationMessageFor(model => model.UserName)
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Password)
                    @Html.ValidationMessageFor(model => model.Password)
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.Email, new { @class = "control-label col-md-2", width = 100 })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Email)
                    @Html.ValidationMessageFor(model => model.Email)
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.Male, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Male)
                    @Html.ValidationMessageFor(model => model.Male)
                </div>
            </div>
    
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Register" class="btn btn-default" />
                </div>
            </div>
        </div>
    }
    
    <div>
        @Html.ActionLink("Log in", "Login")
    </div>
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
        <script>
            $("#form").on("submit", function (event) {
                event.preventDefault();
                if ($("#form").validate()) {
                    $.post("/register", $("#form").serialize(), function () {
                        window.location.href = '@Url.Action("Login")';
                    });
                }
            })
        </script>
    }
    

    But when I press button and my fields is empty, I can see for a millisecond Error messages of unobtrusive near fields, but I am redirecting on "Login" page always.
    How can I fix it?

    Tuesday, December 24, 2013 10:17 AM

Answers

  • User397057121 posted

    If must be:

    if ($("#form").validate().form())



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 25, 2013 7:46 AM

All replies