locked
Parameter Passing From javascript To C# MVC Controller RRS feed

  • Question

  • User974100899 posted

    I am taking a JavaScript course at my local trade school to get better at it. This is an intro course....and we have been passing data from JS to our Controller like this 

    for (i = 0; i <= AddCount; i++) {
    var saveInfo = {
    Address: $("#streetAddress" + i).val(),
    City: $("#City" + i).val()
    };
    saveData(saveInfo);




    now our bonus for the week is to translate this example into the above syntax.... 

    $(this).find("td").each(function() {
    if($(this).has('input[type="checkbox"]').length>0){
    if($(this).find('input').is(':checked')) {
    beenVerified ='on'; 
    }else{
    beenVerified ='off'; 
    }
    }
    });
    if($(this).has('input.streetAddress').length>0){
    streetAddress = $(this).find('input.streetAddress').val(); 
    }
    
    update(streetAddress, beenVerified); 
    

    My thought is to code it just like this but I"m not sure if I fully understand.... 

    var saveInfo = { beenVerified: beenVerified, streetAddress: streetAddress };
    update(saveInfo);



     Does this look right to people more versed in JavaScript?

    I think what needs to be done is to not pass in individual variables, but to pass in an array that contains all the variables. Which is why I did it the way I did in the 3rd code sample....

    Thursday, October 31, 2019 12:11 AM

Answers

  • User-719153870 posted

    Hi LiarLiarPantsOnFire,

    Does this look right to people more versed in JavaScript?

    I think you are right, if i'm not understanding anything here.

    After you get the value from the second part of code, set the value as your third part of code and use this object saveInfo in your update(obj) method.

    I did a little test at Object initializer, it seems works well.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 31, 2019 5:27 AM