我使用了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值预计
我有错误的图像。但我只有7点。所以我无法添加该图像。所以我稍后会把那张图片
答案 0 :(得分:2)
您无法从javascript设置Model对象的属性,原因很简单,您的Model存在于服务器上,而javascript则在客户端上执行。让javascript修改某些服务器端值的唯一方法是向服务器发送请求。有很多方法可以做到这一点,其中一种方法涉及使用AJAX。