Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

working with bool value from View in javascript

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.

like image 616
user1260827 Avatar asked Oct 22 '25 04:10

user1260827


1 Answers

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.

like image 150
Steen Tøttrup Avatar answered Oct 23 '25 20:10

Steen Tøttrup



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!