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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With