Jean Tehhe
Jean Tehhe

Reputation: 1347

Check if some property have values

I'm using the following code which is working as expected. I have two text boxes which change according to a dropdown value(SelType).
I added the last two lines of code that when the user run the page for first time the text box will be disabled.
My question: is there an option to check if something (any property) in the model is having value then execute this code (last two lines)?

 $(document).ready(function () {
        $('select[name="SelType"]').change(function () {

            if ($(this).val() === 'A') {

                $('input[name="Emp.User"]').prop("disabled", true);
                $('input[name="Emp.Password"]').prop("disabled", true);

            }

            else {
                $('input[name="Emp.User"]').prop("disabled", false);
                $('input[name="Emp.Password"]').prop("disabled", false);

            }

        });
//Added code for the init page
        $('input[name="Emp.User"]').prop("disabled", true);
        $('input[name="Emp.Password"]').prop("disabled", true);

Upvotes: 0

Views: 57

Answers (2)

mr100
mr100

Reputation: 4428

Just do it like this:

if(@Model.Condition == true)
{
    $('input[name="Emp.User"]').prop("disabled", true);
    $('input[name="Emp.Password"]').prop("disabled", true);
}

In case the script is located in separate file just store the required value in a Hidden Field on a page like that:

@{
    bool isConditionPropertyNotNull = Model.Text != null;
}
@Html.Hidden("IsConditionPropertyNotNull", isConditionPropertyNotNull)

and then refer to it from the script:

if ($('input[name="IsConditionPropertyNotNull"]').val() == "True") {

}

Upvotes: 1

user3383479
user3383479

Reputation:

You can use something like this:

    @Html.TextBoxFor(m=>m.User, new {disabled="disabled"})

Regarding inside the model I am not sure have seen it. But you read the list of DataAnnotation In your case you can create a custom.

For the password, there is one annotation you can use:

 @Html.PasswordFor()

Upvotes: 1

Related Questions