How to get bool value from model

I need to create a Javascript validation that will compare values from text-boxes against values in the model.

($("#Description").val() != "<%= Model.Description %>")

Description is a string in the model, and code above works fine for it.

($("#Weight").val() != <%= Model.Weight %>)

Weight is a int , and works fine.

And finally if I try to check the bool value it doesn't work at all. Any ideas?

($("#PackingFlag").is(':checked') != "<%= Model.PackingFlag %>")

Answers


$("#PackingFlag").is(':checked')

returns a bool.

"<%= Model.PackingFlag %>"

is a string (hence the ")

bool != string always returns true.


if Model.PackingFlag returns the string true or false then removing the " should result in the correct comparision:

($("#PackingFlag").is(':checked') != <%= Model.PackingFlag %>)

client side this results in:

 ($("#PackingFlag").is(':checked') != true)

Or

 ($("#PackingFlag").is(':checked') != false)

Since right hand side of the comparison is a string, you could try converting the left hand side to a string.

(String($("#PackingFlag").is(':checked')) != "<%= Model.PackingFlag %>")

Need Your Help

Does the Date object ever use a non-Gregorian calendar?

javascript date calendar

Does the Date object in Javascript ever use a non-Gregorian calendar?