no implicit conversion between string and int in razor view javascript area - razor

<script type="text/javascript">
var destinationId = #(Model.DestinationId == 0?"":Model.DestinationId);
</script>
I want to output "" if Model.DestinationId is 0 else display Model.DestinationId

Because your C# code is trying to return a string when the if condition yields true and returns an int(value of DestinationId) when the condition expression returns false. The compiler is telling you that it is not valid! You need to return the same type from both the cases.
To fix the issue, return the same type from both the cases. You can use ToString() method on the int type property so that your ternary expression will always return the same type (string)
var destinationId = #(Model.DestinationId == 0 ? "" : Model.DestinationId.ToString());
Although the above will fix your compiler error, it might not be giving what you are after. The above code will render something like this when your DestinationId property value is 0.
var destinationId = ;
Which is going to give you a script error
Uncaught SyntaxError: Unexpected token ;
and when DestinationId property has a non zero value, for example, 10.
var destinationId = 10;
There are multiple ways to solve the issue. One simple approach is to replace the empty string with something JavaScript understands. Here in the below sample, I am rendering null as the value
var destinationId = #(Model.DestinationId == 0 ? "null" : Model.DestinationId.ToString());
if (destinationId===null) {
console.log('value does not exist');
}
else {
console.log('value is '+ destinationId);
}
Another option is to simply read the DestinationId property value and check for 0 in your JavaScript code as needed.
Another option is to wrap your entire C# expression in single or double quotes. But then again you number value will be represented as a string :(, which is not good.
I suggest you to use the correct type(even in JavaScript)

Related

Trying to check the value of a cell and compare it to another value to see if it matches(Google Script)

I am trying to creating a booking system, and as of now I am going to allow my user to insert their name and then check if there is a value. But whenever I try to simply accomplish this by using if( m == 'string) it thinks that all of the empty spaces are strings which results in everything saying booked.
Function checkifBooked(name)
{
var string ='BOOKED';
var string2 ='FREE';
if(typeof name == 'string')
{
return string;
}
else{
return string2;
}
}
When you get the value of an empty cell you get an empty string '', which is still a string. Thus, your if always evaluates to True.
In javascript, an empty string evaluates to false, so you can test the string directly.
try:
if(name)
Or if you prefer to be more explicit, you could check the type of name, then check it's length.
if(typeof(name) == 'string' && name.length > 0)

Angular2 IndexOf Finding and Deleting Array Value

Hello I'm attempting delete a certain index in my array while using Angular2 and Typescript. I would like to retrieve the index from the value.
My array is declared normally...
RightList = [
'Fourth property',
'Fifth property',
'Sixth property',
]
I start out with a basic premise to set up my remove function.
removeSel(llist: ListComponent, rlist:ListComponent){
this.selectedllist = llist;
console.log(JSON.stringify(this.selectedllist)); // what is to be searched turned into a string so that it may actually be used
My console.log of my JSON.Stringify tells me that the value that I will attempt to remove is "Sixth property". However when I attempt to look for this value in my array using the following code. It returns -1 which means my value is not found in the array.
var rightDel = this.RightList.indexOf((JSON.stringify(this.selectedllist))); // -1 is not found 1 = found
console.log(rightDel);
On my output to the console it does return the item that is to be searched for but does not find the item in the array
CONSOLE OUTPUT:
"Sixth property" // Item to be searched for
-1 // not found
Is there something wrong in my implementation of my function that searches the array?
Of course indexOf will not find you item in array because
JSON.stringify(this.selectedllist) !== this.selectedllist
This is because JSON stringified string encodes quotes around literal, that original string doesn't have. It is easy to test:
var a = 'test';
console.log( JSON.stringify(a), a, JSON.stringify(a) === a )
Remove JSON.stringify and it should work. In general, to cast something to String type you should use its .toString() method, or simply wrap this something into String(something).

ExtJS 4 How to get Form value as Integer

I'm using Ext.JSON.encode(this.getUserForm().getForm().getValues()) to encode my form values in Json fomat.
Here is the probleme : All values are encoded as string, even integer values.
{
"userId":"15",
"username":"Albert",
"firstName":"Albert",
"lastName":"Einstein"
}
the user id a HiddenFiled
How to make my user ID field encoded as Integer.
Thanks
Hidden field is " typeless" so it doesn't condition or convert its value. You have two main options:
Extend the hidden field to implement getValue method that would return integers instead of strings
Use params instead of hidden fields to send id data to the server
try
this.getUserForm().getForm().getFieldValues()
getValues returns only String values for submission
I cant understand the question at first, your number is in "_" may be you need some javascript
isNaN('fds5sdf') returns true
isNaN('15') returns false
so if isNaN function return false you have to parts to Int, parseInt('15');
at first try getFieldValues() if your item in form is xtype: 'numberfield' getFieldValues reurns integer value...
Thank you Saki.I decided to go for the first option.
Ext.define('MA.view.custom.HField', {
extend: 'Ext.form.field.Hidden',
xtype: 'hfield',
getValue: function () {
var value;
var v = this.getSubmitValue();
value = parseInt(v);
return value;
}
});

if ( object != null ) instead of if ( object ) in AS3

Is there any reason to use if ( object != null ) in a conditional instead of the more concise if ( object ). I see the former more often, but it seems like the two are equivalent and the latter shorter.
different objects are treated differently. there are some values that can never be null and might erroneously trigger false when you don't want it to.
Booleans can never be null.
if a Boolean is defined but not given a value it is treated as false;
var bool:Boolean;
trace(bool); // false
bool = true;
trace(bool); // true
int can never be null as well.
if an int is defined but not given a value, it is treated as 0;
var i:int;
trace(i); // 0
trace(Boolean(i)); // false
i = -1;
trace(Boolean(i)); // true
i = 0;
trace(Boolean(i)); // false
i = 1;
trace(Boolean(i)); // true
Number act similar to int.
if a Number is defined but not given a value, it is treated as NaN
var num:Number;
trace(num); // NaN
trace(Boolean(num)); // false
num = -1.1;
trace(Boolean(num)); // true
num = 0;
trace(Boolean(num)); // false
num = 1.1;
trace(Boolean(num)); // true
Strings are the worst.
if a String is defined but not given a value, it is treated as null!
var str:String;
trace(str); // null
trace(Boolean(str)); // false
str = ""
trace(Boolean(str)); // false
str = "hello";
trace(Boolean(str)); // true
it is very rare in my code that i only do 'if (obj)' since most of the time there is something more specific that i truly care about.
The following code will have an object evaluate to false, even though the object is not null:
class SomeWrapper
{
var value: Object;
SomeWrapper(Object value)
{
this.value = value;
}
/// Overriden method from Object, see ActionScript 3 reference
function valueOf()
{
return value;
}
}
var myWrapper = new SomeWrapper(false);
if(myWrapper)
{
trace("myWrapper evaluates to true.");
}
else
{
trace("myWrapper evaluates to false.");
}
The else block will execute in the example above, because myWrapper evaluates to false (its valueOf method returns whatever value the wrapper contained, in this case false) even though myWrapper is not a null reference. The problem is that the above does not only test nullability of reference, it implicitly invokes valueOf method, courtesy of Flash Player virtual machine. This behavior of course may or may not be what you wanted - do you want to test whether myWrapper is a null value or whether it carries a null value? (or both perhaps).
Verbosity in this case is your friend, otherwise you gain code readability in return for potential runtime errors.
if (expression) tests the truthiness of expression by getting its boolean value. Since null and undefined are both falsy, if (object) "works" for making sure object isn't null.
Problem is, though, 0, false, and '' are also falsy. When you use if (object), the code you're trying to keep from running when it's null would also be ignored when object is zero, an empty string, or the actual boolean false. Often, that's not what you want.
If you can't guarantee that object is either null or a type other than string, number, or boolean (or their respective object types), then if (object != null) is less likely to mean something unintended.

JSON empty string

why does the JSON.stringify-Function converts a string.Empty ("") to a "null"-String?
The problem, why i'm not using:
JSON.parse(json, function(key, value) {
if (typeof value === 'string') {
if (value == 'null')
return '';
return value;
}
});
...is, if somebody really write "null" (is very unlikely, but possible), i have a problem to...
thank for each answer!
Old question - but its the top result when you search for 'json stringify empty string' so I'll share the answer I found.
This appears to be a bug in certain versions of IE8, where empty DOM elements return a value which looks like an empty string, evaluates true when compared to an empty string, but actually has some different encoding denoting that it is a null value.
One solution is to do a replace whenever you call stringify.
JSON.stringify(foo, function(key, value) { return value === "" ? "" : value });
See also http://blogs.msdn.com/b/jscript/archive/2009/06/23/serializing-the-value-of-empty-dom-elements-using-native-json-in-ie8.aspx
now the esiest solution for this problem is, to pack the "document.getElementById('id').value" expression in the constructor of the String class:
JSON.stringify({a:new String(document.getElementById('id').value)}); -> {"a":""}
i can't find the primary problem, but with this, it's working well in Internet Explorer as well in FireFox.
i'm not very happy with this dirty solution, but the effort is not to much.
JSON library: https://github.com/douglascrockford/JSON-js/blob/master/json2.js