-1

I am receiving this value from the server code in the cshtml file. Its a boolean field and I get boolean value.

Order="@Model.OrderModel.IsToday"

I tried to do this, but unfortunately it says it will be a string when I hover over ToJson.

Order="@Model.OrderModel.IsToday.ToJson()"

How to send this as bool to javascript? I need to explicit cast but my syntax of using bool is wrong I guess.

EDIT

When I use this Order="@Model.OrderModel.IsToday" in the javascript it says undefined, freaking!

Also tried this

Order="@Json.Encode(Model.OrderModel.IsToday)"

Still sends as string. I don't want to send it as string as its a boolean

Jasmine
  • 5,186
  • 16
  • 62
  • 114
  • 1
    Can you provide more context to your code? Is `Order` a part of JavaScript code and you are trying to assign a value from the server side? – dotnetom Sep 15 '16 at 03:57
  • @dotnetom: It's simple mate, nothign more you need to understand or provide a solution for what I am after. It sends the boolean value as "true" I want it to be sent as a boolean i.e true – Jasmine Sep 15 '16 at 04:02
  • As @dotnetom said, what exactly are you trying to do? Wild guess -> I've had issues where C# shows bool as "True" from `ToString()`, which didn't actually map to `true` in JavaScript, so I needed to call `.ToLower()` or similar. – Don Cheadle Sep 15 '16 at 04:02
  • Maybe because you're putting it in a string... http://stackoverflow.com/questions/14448604/using-razor-how-do-i-render-a-boolean-to-a-javascript-variable – Don Cheadle Sep 15 '16 at 04:03
  • @mmcrae: I have this sent as string from cshtml file to javascript file. This is a boolean field. I want to, hence, send as a boolean. Do you understand this layman english at least? – Jasmine Sep 15 '16 at 04:07
  • @mmcrae: I already followed the link you gave and then only came here. That link doesn't help me as it still send the value as string – Jasmine Sep 15 '16 at 04:08
  • @Learner ok so `IsToday` is a `bool` in a C# class. But what is Order? How/where are you trying to pass it to JavaScript? – Don Cheadle Sep 15 '16 at 04:11
  • @mmcrae: Mate you dont understand even my english. – Jasmine Sep 15 '16 at 04:51

2 Answers2

1

You get string value in JavaScript, because you are using quotes in your variable, so anything you have inside becomes a string.

Try this instead:

Order = @Model.OrderModel.IsToday.ToString().ToLower()

Here you convert Boolean value to string, and then convert it to lower case. You need it, because ToString would give you True or False, and you need the Boolean value to be true or false.

dotnetom
  • 24,551
  • 9
  • 51
  • 54
  • No, I already have this solution, but my lead says it shouldn't be sent as string. In the Javascript I did like ctrl.OrderToday = Order ==='true' but he says it is not a good idea or approach. I am after sending it as a boolean. Can you make it? – Jasmine Sep 15 '16 at 04:05
  • See this, what you wrote is not a good practise, see this post http://stackoverflow.com/questions/11733120/working-with-bool-value-from-view-in-javascript – Jasmine Sep 15 '16 at 04:06
  • If `IsToday` is a `bool`, then why doesn't the example in that Answer work for you? i.e. `@Json.Encode(Model.IsToday)` – Don Cheadle Sep 15 '16 at 04:12
0

Pass it as a string and convert to boolean in Javascript:

var myBool = Boolean(@Model.OrderModel.IsToday.ToString().ToLower());

See here

Community
  • 1
  • 1
Shiham
  • 2,114
  • 1
  • 27
  • 36