L-value is expected javascript

13,467

You cannot set properties of your Model object from javascript for the very simple reason that your Model lives on the server, whereas javascript executes on the client. The only way to have javascript modify some server side value is to send a request to the server. There are many ways to do that, one of which involves using AJAX.

Share:
13,467
Admin
Author by

Admin

Updated on August 15, 2022

Comments

  • Admin
    Admin over 1 year

    I used MVC4 ,I want to set values in property of model used Javascript

    I tried this code

    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 + "%");
    
             }
        }
    

    but this line "@Model.complianceTypeId" threw this error L- value is expected

    i have the error image .But i have just 7 point.so i can't add that image .So i will put that image later

  • Darin Dimitrov
    Darin Dimitrov almost 11 years
    That's because you are attempting to assign the right hand value of the expression to a string literal on the left which doesn't make any sense. The following is completely invalid javascript: "some string" = someValue;. That's what you wrote.
  • Admin
    Admin almost 11 years
    Buti used same line in my if condition .This line not return error . why @DarinDimitrov
  • Darin Dimitrov
    Darin Dimitrov almost 11 years
    Take a look at what's generated in the resulting HTML. You will see that: if ("123" != 11 && && academicAchievement != "" && academicAchievement != undefined && academicGrowth != "" && academicGrowth != undefined && academicGrowthGaps != "" && academicGrowthGaps != undefined) which is valid javascript. Except that you are comparing a string to an integer "123" != 11 which is a very bad test. When you write @Model.complianceTypeId, this is executed on the server, and evaluated on the server. On the client you get only the result of this evaluation. There are no models on the client.
  • Darin Dimitrov
    Darin Dimitrov almost 11 years
    Browse the generated HTML as seen by your webbrowser. That's what gets executed by javascript.
  • Admin
    Admin almost 11 years
    ya !i saw that ! its display like "11"==11 . I understand .. Thanks
  • Admin
    Admin almost 11 years
    How can i set value in javascript ?? Can i do this using hiddenfield ? @Darin
  • Darin Dimitrov
    Darin Dimitrov almost 11 years
    You could have a hidden field and then use javascript to set the value of this hidden field. Then when the form is submitted to the server you will be able to retrieve the value of this hidden field on the server.