4

I have a bool property in a model:

public bool IsExistSchedule {get; set;}

In the View I wrote the following:

<div id="step1" data-schedule="@Model.IsExistSchedule">
...
</div>

In the generated page data-schedule contain True value.
How to properly work with this value in javascript?
For example, I want to check if variable is true:

var isExist = $('#step1').attr('data-schedule');
if( isExist === "True")
     do something

I don't like this part isExist === "True". I should convert it to bool may be or something other. I'm afraid that in other browser the variable may contain other values: true, True, "True", "true".
Thanks.

2 Answers 2

10

Generally if you need any of your model for javascript, it's a good idea to do this in you views:

@Json.Encode(Model.IsExistSchedule)

This will "convert" the value into something javascript will understand, like lower-casing a boolean.

And then, like @Musa suggests, use the .data() method.

Sign up to request clarification or add additional context in comments.

Comments

2

The .data() method will convert it to boolean, it is compatable with html5 data attributes

var isExist = $('#step1').data('schedule');
if (isExist)
    do something

Note "True" does not convert to boolean(true) so it has to be "true"

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.