Click here to Skip to main content
15,886,078 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So this is what happens.
I'm pulling data from db to show it on application.
I save it to ViewModel and all data is there, so no problems with queries or anything backend-wise.

So I used html helper to show data.

@Model.SelectedVehicle.VehicleId

When I wrote it out on HTML, it was okay as you can see here. http://imgur.com/7uZbAnr

But problem starts when I write it out using JS (alert or console.log), cuz it somehow or somewhat changes value of data as you can see here. http://imgur.com/bFA9E6I

On console log screen show 12 is VehicleId which is supposed to be 0014 as you could see on HTML screenshot, where 1234 is DriverId and I have no issues displaying it either way.

I have no idea how or why this is happening, so if someone could explain me what might be causing it, I'd be grateful.

What I have tried:

I tried figuring it out by myself but I cannot solve it, cause that number 12 is nowhere is db.

On other vehicle where Id is 0000, JS writes out 0.

EDIT:

Controller:
[HttpPost]
        [Authorize]
        public ActionResult SelectedVehicle(DashboardViewModel model)
        {
            var userId = User.Identity.GetUserId();
            var currentDriver = context.Drivers
                .Where(m => m.AccountId == userId && m.DriverId == model.DriverId)
                .Select(c => c)
                .FirstOrDefault();
            var currentVehicle = context.Vehicles
                .Where(m => m.AccountId == userId && m.VehicleId == model.VehicleId)
                .Select(c => c)
                .FirstOrDefault();

            var viewModel = new DashboardViewModel
            {
                currDriver = currentDriver,
                currVehicle = currentVehicle
                
            };
            return View("Index", viewModel);
        }


ViewModel:

public class DashboardViewModel
   {
       public DashboardViewModel()
       {
           this.currDriver = new Driver();
           this.currVehicle = new Vehicle();
       }

       public Driver currDriver { get; set; }
       public Vehicle currVehicle { get; set; }

   }


HTML part which works normal:
<div class="x_content">
                Being tracked now: 
                "vehicle-name">@Model.currVehicle.RegistrationPlates
                driven by 
                ^__strong id="driver-name">@Model.currDriver.Firstname @Model.currDriver.Lastname 
            </div>


JS part:
<script>
    
    $('a.pick-this').on('click', function () {
        console.log(@Model.currVehicle.VehicleId);
        console.log(@Model.currDriver.DriverId);
    });
    
</script>


HTML SOURCE:
<div class="x_content">
                Being tracked now: 
                "vehicle-name">222-B-222
                driven by 
                ^__strong id="driver-name">Faris Karcic 
            </div>


JS SOURCE:
<script>
    $('a.pick-this').on('click', function () {
        console.log(0014);
        console.log(1234);
    });
    
</script>


As I have noticed now, values in JS are right, but why I'm getting in console instead of 0014 number 12 still?

I just wanna learn why this happens...
Posted
Updated 10-Aug-17 9:07am
v4
Comments
Sinisa Hajnal 10-Aug-17 8:39am    
Cannot access the images. In general, it is bad practice to have random links on the forum as people tend to be wary of unknown sources.
Faris Karcic 10-Aug-17 8:43am    
Hope its accessible now, I updated it with imgur links
Richard Deeming 10-Aug-17 8:48am    
Update your question to show the relevant parts of your code, and the actual rendered output copied from the browser's "View source" option.
F-ES Sitecore 10-Aug-17 8:49am    
It's happening because of a fault in your code.
Faris Karcic 10-Aug-17 14:35pm    
Updated!

@F-ES Sitecore That seems kind of rude, since I'm sure my code is alright, if I dont know something I say that it's me and that I dont know how to do it. Please dont take me for an idiot.

1 solution

Surround the values in quotes:
console.log('@Model.currVehicle.VehicleId');
console.log('@Model.currDriver.DriverId');

At the moment, Javascript is treating the value as a numeric literal, and will not preserve any leading zeros.

What's worse, in some (most) browsers, the leading 0 will cause Javascript to treat the value as Octal[^]. 14 in Octal is 12 in decimal.

Numbers and dates - JavaScript | MDN[^]
 
Share this answer
 
Comments
Faris Karcic 10-Aug-17 15:42pm    
Oh I get it now. Thanks alot for explanation!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900