Getting an array item property in javascript

时间:2018-02-01 18:32:36

标签: javascript c# asp.net asp.net-mvc

My question revolves around appending items to a dropdownlist based upon choices made on a previous dropdownlist. Here's my code in JS:

function propogateProgramListOptions(data) {
    $.post('PostProgramData',
        { clientId: data },
        function(result) {
            var programs = result.data;
            for (var i = 0; i < programs.length; i++) {
                alert(programs[i]);
            }
        });
     }

Here's my code in C#:

public JsonResult PostProgramData(string[] clientId) {
        string domainUserName = GetDomainUserName();
        ProgramRef[] programsArray = _manager.GetCPRPrograms(domainUserName, clientId);
        List<ProgramRef> programsList = programsArray.ToList();
        return Json(new {data = programsList}, JsonRequestBehavior.AllowGet);
    }

Now each ProgramRef has a property of ProgramCode and ProgramName. Usually in C#, you can just iterate through the array and do programsArray[i].ProgramCode etc. But in this case i'm returning it as a Jsonresult. How would i access the ProgramCode and ProgramName properties of the returned item in JS and do result[i].programName?

1 个答案:

答案 0 :(得分:0)

得到了答案。

programs[i].ProgramCode
programs[i].ProgramName

我认为这不会起作用,因为我的IDE抱怨它是一个全局变量。