0

Using MVC.. when user hit's Create button and a specific field is null or white space, then display dialog box to confirm. If field has value, do not display dialog box and go straight to HttpPost Controller Method..

Here is what I have so far:

CSHTML:

<div class="form-group">
    @Html.LabelFor(model => model.ATime, "ATime:", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.ATime, new { htmlAttributes = new { id = "AValue", @class = "form-control a-textbox", @placeholder = "xxxx.x" } })
        @Html.ValidationMessageFor(model => model.ATime, "", new { @class = "text-danger" })
    </div>
</div>

<div id="dialog" title="Basic dialog">
    <p>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>
</div>

<input type="submit" value="Create" class="btn btn-primary btnSubmitDS" />

JavaScript:

$(document).ready(function () {
    $(".btnSubmitDS").on("click", function (e) {
        var aValue = $("#AValue").val();

        alert(aValue);

        if (aValue == null || aValue.length() == 0) {
            $(function () {
                $('#dialog').dialog();
            });
        }
    });
});

CSS:

#dialog{
    display:none;
}

Now, the correct value is being alerted to the user when the button is clicked but throws an error at the if statement in the JS saying:

Function Expected

I have tried to recreate this in a JSFiddle and the correct value is being alerted, but the dialog box is not appearing there either, and I did include the jQuery library.

Any help is appreciated.

1
  • 1
    if (aValue == null || aValue.length === 0) { $('#dialog').dialog(); }. in the click handler. length is property not method. Also, $(function () { is short form of ready() and not required in the handler. Commented Jul 5, 2016 at 13:53

2 Answers 2

2

Use aValue.length instead of aValue.length()

 $('#SubmitBTN').on("click", function (e) {
    var value = $('#TextBox').val();
    if(value == null || value.length == 0){
        $(function() {
            $('#dialog').dialog();
        });
    }
  });
Sign up to request clarification or add additional context in comments.

1 Comment

okay that worked, now when I run, i get an error saying it doesn't support property or method 'dialog' and I am loading the jQuery before this script is ran..
1

use aValue.length instead of aValue.length(). and add jquery UI after the jquery file. ''

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.