Copied to clipboard

Flag this post as spam?

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


  • Damien Green 72 posts 134 karma points
    Aug 12, 2014 @ 21:51
    Damien Green
    0

    Best way to show a Javascript error message when submitting a SurfaceController Form.

    Hi Guys,

    I have a simple contact form that sends an email to the website owner based on contact details entered into the form. If there is a problem sending the data to the website owner's email e.g. SMTP password is incorrect or the SMTP server is down, I would like to display the error message as a JavaScript dialog (the email send code is written in C# code behind).

    I know I can use something like:

    return Content("

    <script>alert('Error sending email: " + ErrorMessage + "');</script>
    ");

    in the ActionResult for the code that handles the form submission (I'm using the BeginUmbracoForm to handle the submission) but i think the above is a bit of a hack and messy. It also doesn't allow me to return the user back to the page in the event of a problem.

    Alternatively I was thinking of placing the error message dynamically on the page itself, but I'm not sure of the best way to accomplish this either. I was thinking along the lines of modifying a property on the page to contain my error message - but it's my understanding that properties are read only...?

    What I would ideally like is to:

    1. execute JavaScript code
    2. execute an action result such as return RedirectToCurrentUmbracoPage()

    I'm sure that must be a preferred way of accomplishing this type of behavior so if anyone has any pointers / examples that would be very useful.

    Many thanks,

    Damien

  • Damien Green 72 posts 134 karma points
    Aug 13, 2014 @ 10:46
    Damien Green
    0

    Hi Guys,

    I found a much better way to do it. Essentially creating a new ActionResult handler and the TempData variable to pass information back to the page from the controller. I was then able to use the TempData variables to change the resulting page and thus hide / show message boxes within the html based on my temp data values.

    Cheers,

    Damien

Please Sign in or register to post replies

Write your reply to:

Draft