ma32112345
ma32112345

Reputation: 67

Dialog does not show in If statement

I have an alert which shows when my form has been saved.

However when I add a dialog it does not work for some reason..

I am using Html.BeginForm and calling onsubmit = "validateForm(event)" as seen below:

        @using (Html.BeginForm("Save", "ReadingsEntry", FormMethod.Post, new { enctype = "multipart/form-data", onsubmit = "validateForm(event)" }))
        {

I have used this alert which works fine when I click submit:

    if (validateForm = true) {
        alert("test");
    }

But if I change this to a dialog it does not work:

    if (validateForm = true) {
        $("#dialog").dialog({
            modal: true,
            buttons: {
                Ok: function () {
                    $(this).dialog("close");
                }
            }
        });
    }

with the html:

 <div id="dialog" title="Basic dialog">
<p>Thank you for submitting this form. This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

Any idea why my alert works but not my Dialog, am assuming its because maybe its not liking it because its in a if statement?

I have also tried it on the validateForm function:

   function validateForm() {
    $("#dialog").dialog({
        modal: true,
        buttons: {
            Ok: function () {
                $(this).dialog("close");
            }
        }
    });
}

Still no luck..

Thanks

Upvotes: 0

Views: 80

Answers (1)

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93571

This is an assignment of true to validateForm:

if (validateForm = true)

So the expression will always be true regardless of what was in validateForm (and it "was" a function)!

this is a test of the result of calling validateForm being equal to true:

if (validateForm() == true)

And this is the preferred way to check a boolean for truthyness:

if (validateForm())

Having said all that, your validateForm does not even return a value, so this entire problem needs a rethink. Can you please explain the overall aim of your code? :)

Ignoring the additional issues, you can test further by adding a return true; to your function (just for now):

function validateForm() {
    ...
    return true
}

then this, at least, will now open the dialog:

if (validateForm()) {
    $("#dialog").dialog({
        modal: true,
        buttons: {
            Ok: function () {
                $(this).dialog("close");
            }
        }
    });
}

Upvotes: 2

Related Questions