KnockoutJS没有绑定数据

时间:2015-10-14 19:10:42

标签: javascript asp.net-mvc knockout.js asp.net-mvc-5

我是淘宝网和ASP.net MVC的初学者。

一切都在本地机器上工作正常但是因为我上传了脚本knockoutJS绑定在某些情况下不起作用,我已经尝试了很多但是没有修复。现在我需要你的帮助。 如果你看到这个url(http://www.cepinceleme.com/Phone/Samsung-Galaxy-J5)评级系统被加载(我使用knockoutJS绑定值),因为设计或功能或性能5.0每个,这是完整的值。但是当你对它进行评分时,任何值都会改变为不满的东西,例如5.3或7.2它将通过knockoutJS绑定到页面加载。

这是一个示例(http://www.cepinceleme.com/Phone/Samsung-Galaxy-S6),其中评级系统没有使用knockoutJS在页面加载上绑定值。

代码低于

<script type="text/javascript">
function AppViewModel() {
    var self = this;
    self.MobileId = ko.observable(7413);
    self.TotalVotes = ko.observable(4);
    self.DesignVal = ko.observable(8,75);
    self.FeatureVal = ko.observable(8,75);
    self.PerformanceVal = ko.observable(8,375);


    var DesignPercentage = 8,75
    self.percentageDesign = ko.observable('<div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="' + (DesignPercentage * 10) + '" aria-valuemin="0" aria-valuemax="100" style="width:' + (DesignPercentage * 10) + '%">' +(DesignPercentage.toFixed(1)) + '</div>');

    var FeaturesPercentage = 8,75
    self.percentageFeatures = ko.observable('<div class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="' + (FeaturesPercentage * 10) + '" aria-valuemin="0" aria-valuemax="100" style="width:' + (FeaturesPercentage * 10) + '%">' + (FeaturesPercentage.toFixed(1)) + '</div>');

    var PerformancePercentage = 8,375
    self.percentagePerformance = ko.observable('<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="' + (PerformancePercentage * 10) + '" aria-valuemin="0" aria-valuemax="100" style="width:' + (PerformancePercentage * 10) + '%">' + (PerformancePercentage.toFixed(1)) + '</div>');

    self.Thanks = ko.observable("");
    self.designvalues = ko.observableArray(["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]);
    self.designSelectedValue = ko.observable("5");

    self.featuresvalues = ko.observableArray(["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]);
    self.featuresSelectedValue = ko.observable("5");

    self.performancevalues = ko.observableArray(["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]);
    self.performanceSelectedValue = ko.observable("5");

    self.vote = function () {
        var mydata = { id: self.MobileId(), totalVotes: self.TotalVotes(), voteDesign: self.designSelectedValue(), voteFeatures: self.featuresSelectedValue(), votePerformance: self.performanceSelectedValue() }
        self.save(mydata);
        self.Thanks("Thanks for voting");
        self.hasClicked(true);
    };


    self.hasClicked = ko.observable('False' != 'False');
    self.save = function (mydata) {
        $.post('/PhoneSingle/VoteSave', mydata, function (data) {
            self.TotalVotes(self.TotalVotes() + 1);

            var newDesignValue = ((self.DesignVal() + parseFloat(self.designSelectedValue())) / 2.0).toFixed(1)
            var newFeaturesValue = ((self.FeatureVal() + parseFloat(self.featuresSelectedValue())) / 2.0).toFixed(1)
            var newPerformanceValue = ((self.PerformanceVal() + parseFloat(self.performanceSelectedValue())) / 2.0).toFixed(1)
            self.percentageDesign('<div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="' + (newDesignValue * 10) + '" aria-valuemin="0" aria-valuemax="100" style="width:' + (newDesignValue * 10) + '%">' + newDesignValue + '</div>');
            self.percentageFeatures('<div class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="' + (newFeaturesValue * 10) + '" aria-valuemin="0" aria-valuemax="100" style="width:' + (newFeaturesValue * 10) + '%">' + newFeaturesValue + '</div>');
            self.percentagePerformance('<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="' + (newPerformanceValue * 10) + '" aria-valuemin="0" aria-valuemax="100" style="width:' + (newPerformanceValue * 10) + '%">' + newPerformanceValue + '</div>');
        });
    };
}
var viewModel = AppViewModel();
ko.applyBindings(viewModel);

设计值为:8.7

功能值为:8.7

效果值为:8.3

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:1)

我在加载问题页面时收到“意外号码”,因为这些数字有逗号而不是小数点。

var DesignPercentage = 8,75