当从数据库中提取数据时,为什么JS会从HTML显示不同的数据值 [英] Why does JS show different data value from HTML when data is pulled from db

查看:114
本文介绍了当从数据库中提取数据时,为什么JS会从HTML显示不同的数据值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这就是发生的事情。

我从数据库中提取数据以在应用程序中显示它。

我将它保存到ViewModel并且所有数据都在那里,所以查询或后端任何问题都没有问题。



所以我用html helper来显示数据。



@ Model.SelectedVehicle.VehicleId



当我在HTML上写出来时,它没关系你可以在这看到。 http://imgur.com/7uZbAnr



但是当我开始出问题使用JS(alert或console.log)写出来,因为你可以在这里看到它,或者某种程度上改变数据的值。 http://imgur.com/bFA9E6I



在控制台日志屏幕上显示12是VehicleId,应该是你在HTML截图中看到的0014,其中1234是DriverId,我没有任何问题以任何方式显示它。



我没有想法如何或为何如此发生,所以如果有人能解释我可能会导致什么,我将不胜感激。



我尝试过:



我试过自己弄清楚但是我无法解决它,因为数字12无处不在。



在Id为0000的其他车辆上,JS写出0.



编辑:



控制器:

 [HttpPost] 
[授权]
public ActionResult SelectedVehicle(DashboardViewModel model)
{
var userId = User.Identity.GetUserId();
var currentDriver = context.Drivers
.Where(m => m.AccountId == userId&& m.DriverId == model.DriverId)
。选择(c => ; c)
.FirstOrDefault();
var currentVehicle = context.Vehicles
.Where(m => m.AccountId == userId&& m.VehicleId == model.VehicleId)
。选择(c => ; c)
.FirstOrDefault();

var viewModel = new DashboardViewModel
{
currDriver = currentDriver,
currVehicle = currentVehicle

};
返回View(Index,viewModel);
}





ViewModel:



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

公共驱动程序currDriver {get;组; }
public Vehicle currVehicle {get;组; }

}





HTML部分正常工作:

< div class =x_content> 
现在被跟踪:
@ Model.currVehicle.VehicleName
@ Model.currVehicle.VehicleId
@ Model.currVehicle.RegistrationPlates

驱动 @ Model.currDriver.Firstname @ Model.currDriver.Lastname
< / div>





JS部分:

<脚本> 

$('a.pick-this')。on('click',function(){
console.log(@ Model.currVehicle.VehicleId);
console .log(@ Model.currDriver.DriverId);
});

< / script>





HTML来源:

< div class =x_content> 
现在被跟踪:
欧宝Tigra
0014
222-B -222

驱动 Faris Karcic
< / div>





JS来源:

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

< / script>





正如我现在注意到的,JS中的值是正确的,但为什么我进入控制台而不是第12号仍然是第12号?



我只是想知道为什么会发生这种情况......

解决方案

('a.pick-this').on('click',function(){
console.log(@ Model.currVehicle.VehicleId);
console.log( @ Model.currDriver.DriverId);
});

< / script>





HTML来源:

< div class =x_content> 
现在被跟踪:
欧宝Tigra
0014
222-B -222

驱动 Faris Karcic
< / div>





JS来源:

< script> 


('a.pick-this')。on('click',function(){
console.log(0014);
console.log(1234);
} );

< / script>





正如我现在注意到的,JS中的值是正确的,但为什么我进入控制台而不是第12号仍然是第12号?



我只是想知道为什么会发生这种情况......


用引号括起值:

 console.log('@ Model.currVehicle.VehicleId'); 
console.log('@ Model.currDriver.DriverId');



目前,Javascript将该值视为数字文字,并且不会保留任何前导零。



更糟糕的是,在某些(大多数)浏览器中,领先 0 将导致Javascript将值视为八进制 [ ^ ]。八进制的 14 <十进制 12



数字和日期 - JavaScript | MDN [ ^ ]


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: 
                @Model.currVehicle.VehicleName 
                @Model.currVehicle.VehicleId 
                @Model.currVehicle.RegistrationPlates
                driven by 
                @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: 
                Opel Tigra 
                0014 
                222-B-222
                driven by 
                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...

解决方案

('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: 
                Opel Tigra 
                0014 
                222-B-222
                driven by 
                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...


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[^]


这篇关于当从数据库中提取数据时,为什么JS会从HTML显示不同的数据值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆