Solution 1 :

You should have tried it, but maybe handling the arg as string for the javascript typeless magic?

function DeleteRow(barCode)
    const str = barCode.split("-")

Solution 2 :

<button type="button" onclick="DeleteRow(@Html.Raw(Model.BarCode))">Click Me!</button>

Solution 3 :

<button type="button" onclick="DeleteRow('@Model.BarCode')">Click Me!</button>

This has solved the problem for me.

Problem :

I have a string 100-10-0 in my MVC model. I have to pass this string to a javascript function on an HTML button click. My code is:

<button type="button" onclick="DeleteRow(@Model.BarCode)">Click Me!</button>

@Model.BarCode has the value “100-10-0”.

My javascript function is:

function DeleteRow(barCode)
    console.log(barCode) // Output is 90

Problem here is that when barcode comes to javascript method as parameter it is converted to number and the minus operator is applied on it, thats why it shows 90 on the console. How can i prevent this behavior ? i want 100-10-0 string and not 90. How can i tell javascript to consider it as string and not as number ?


Comment posted by Waleed Naveed

In the parameter, i get 90, i have tested in by putting debugger at the first line of function. So, splitting 90 (number) at “-” would not work.

Comment posted by stealththeninja

Please improve this answer by explaining how the code snippet is different from the question, and why this solves the problem.