4

我使用了 MVC4,我想在使用的 Javascript 模型的属性中设置值

我试过这段代码

function updateTotals() {

        debugger;
        var academicAchievement = $('#AcademicAchievement').val();
        var academicGrowth = $('#AcademicGrowth').val();
        var academicGrowthGaps = $('#AcademicGrowthGaps').val();
        var postsecondarAndWorkforceReadiness = $('#PostsecondarAndWorkforceReadiness').val();

        if ("@Model.complianceTypeId" != 11 && academicAchievement != "" && academicAchievement != undefined && academicGrowth != "" && academicGrowth != undefined && academicGrowthGaps != "" && academicGrowthGaps != undefined) {
            $('#Total').val("");
            $('#Total').val(((parseInt(academicAchievement, 10) + parseInt(academicGrowth, 10) + parseInt(academicGrowthGaps, 10)) / 100) * 100 + "%");
            $('#Total').attr("disabled", "disabled");
            $('#TotalValuehdn').val((((parseInt(postsecondarAndWorkforceReadiness, 10) + parseInt(academicAchievement, 10) + parseInt(academicGrowth, 10) + parseInt(academicGrowthGaps, 10)) / 100) * 100 + "%"));
        }

        if ("@Model.complianceTypeId" == 11 && postsecondarAndWorkforceReadiness != "" && postsecondarAndWorkforceReadiness != undefined   && academicAchievement != "" && academicAchievement != undefined && academicGrowth != "" && academicGrowth != undefined && academicGrowthGaps != "" && academicGrowthGaps != undefined) {
             $('#Total').val("");
             $('#Total').val((((parseInt(postsecondarAndWorkforceReadiness, 10) + parseInt(academicAchievement, 10) + parseInt(academicGrowth, 10) + parseInt(academicGrowthGaps, 10)) / 100) * 100 + "%"));
             $('#Total').attr("disabled", "disabled");
             **"@Model.complianceTypeId"**= (((parseInt(postsecondarAndWorkforceReadiness, 10) + parseInt(academicAchievement, 10) + parseInt(academicGrowth, 10) + parseInt(academicGrowthGaps, 10)) / 100) * 100 + "%");

         }
    }

但是这一行 “@Model.complianceTypeId”抛出了这个错误L- value is expected

我有错误图像。但我只有 7 点。所以我不能添加那个图像。所以我稍后会放那个图像

4

1 回答 1

3

您不能从 javascript 中设置 Model 对象的属性,原因很简单,因为您的模型位于服务器上,而 javascript 在客户端上执行。让 javascript 修改某些服务器端值的唯一方法是向服务器发送请求。有很多方法可以做到这一点,其中之一涉及使用 AJAX。

于 2013-06-19T11:25:12.600 回答