Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 23, 2015 @ 19:16
    Michaël Vanbrabandt
    0

    Update HTML after Form submit using Ajax

    Hi,

    I am using Umbraco 7 for one of my websites. I am currently creating the login/logout section of the website.

    Everything is working fine except after the user enters wrong credentials I need to show a new div tag. I use TempData to store the error and the error is their when I debug.

    Except It won't refresh the HTML because I am using jQuery Ajax to submit the form.

    jQuery code:

    $(function () {
        $('form').submit(function () {
            if ($(this).valid()) {
                $.ajax({
                    url: this.action,
                    type: this.method,
                    data: $(this).serialize()
                });
            }
            return false;
        });
    });
    

    Controller:

    [HttpPost]
    public ActionResult MemberLogin(LoginFormViewModel model)
    {
        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.Username, model.Password))
            {
                //FormsAuthentication.SetAuthCookie(model.Username, false);
    
                return RedirectToCurrentUmbracoPage();
            }
            else
            {
                TempData["LoginError"] = true;
                return RedirectToCurrentUmbracoPage();
            }
        }
        else
        {
            return RedirectToCurrentUmbracoPage();
        }
    }
    

    View:

    @if (TempData["LoginError"] != null)
    {
        <div class="form-group">
            <span class="field-validation-error">
                 <span>Gebruikersnaam of wachtwoord onjuist!</span>
            </span>
        </div>
    }
    else
    {
        <div class="form-group">
            <span>Correct!</span>
        </div>
    }
    

    Is this normal behaviour of the ajax submit? Do I need to use the error() handler of the jQuery ajax to handle the error message?

    /Michael

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Mar 24, 2015 @ 10:35
    Alex Skrypnyk
    100

    Hi Michaël,

    Yes, this is normal behavior of your code. I prefer to you use some @Html.AntiForgeryToken() in the form code, and verification attribute in the controller, it will be more secure.

    Another thing, you have return RedirectToCurrentUmbracoPage();, this code isn't the best way to get answer from the server if you are using ajax submit. Try to use json, or http response codes.

    Thanks, Alex

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 24, 2015 @ 10:46
    Michaël Vanbrabandt
    0

    Hi Alex,

    thanks for your reply.

    So instead of using the TempDate I should return some json object with the error or success message and then display this in the disered HTML?

    /Michael

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Mar 24, 2015 @ 10:47
    Alex Skrypnyk
    0

    Michael,

    Yes json will be better, and it will be faster, but please do not forget about security code )) I think it's important.

    Thanks

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 24, 2015 @ 10:49
    Michaël Vanbrabandt
    1

    Alex,

    so for every form you implement you have to define the @Html.AntiForgeryToken() and in your controller add the [ValidateAntiForgeryToken] attribute to be more secure?

    /Michael

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Mar 24, 2015 @ 10:51
    Alex Skrypnyk
    0

    Yes, it detect and block CSRF using the “user-specific tokens” technique. You can replace it with captcha.

     

    /Alex

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 24, 2015 @ 10:52
    Michaël Vanbrabandt
    0

    Alex,

    ok its clear to me.

    Thanks

    /Michael

Please Sign in or register to post replies

Write your reply to:

Draft