使用Jquery在Object内推送Object

时间:2018-04-09 10:51:05

标签: javascript jquery asp.net-mvc-5

我的课程如下。

public class ProjectConfigModel
{
    ProjectDetailModel MCRIDInfo { get; set; }
    List<string> DoorsPaths { get; set; }
    public List<string> TrackNReleasePrjName { get; set; }
    public List<string> ProjectProxies { get; set; }
    public string Comments { get; set; }
}

我需要使用Jquery填充此类对象并将其传递给我的控制器。

public JsonResult ConfigureProject(ProjectConfigModel PrjConfigObj)
{
}

下面是我使用Jquery填充对象的方法。

        function onFinishCallback(onSucess, onError)
        {
            var ProjectInfo = {};
            var MCRIDInfo = {};
            MCRIDInfo["MCRID"] = $("#LBL_McrID_Summry").text();
            MCRIDInfo["ProjectName"] = $("#LBL_ProjName_Summry").text();
            MCRIDInfo["ProgramID"] = $("#LBL_PID_Summry").text();
            MCRIDInfo["Division"] = $("#LBL_Division_Summry").text();
            MCRIDInfo["ProjectManager"] = $("#LBL_PJM_Summry").text();
            MCRIDInfo["Generation"] = $("#LBL_Gen_Summry").text();
            MCRIDInfo["Region"] = $("#LBL_Region_Summry").text();
            MCRIDInfo["ProjectType"] = $("#LBL_McrID_Summry").text;
            MCRIDInfo["BusinessUnit"] = $("#LBL_McrID_Summry").text;
            MCRIDInfo["ProductClass"] = $("#LBL_McrID_Summry").text;
            ProjectInfo.push(MCRIDInfo);
          //  $.extend(ProjectInfo, MCRIDInfo);

            var DoorsPaths = $("#TA_DorsPath_Summry").text().split(';');
            var TrackNReleasePrjName = $("#TA_TRPrjName_Summry").text().split(';');
            var ProjectProxies = $("#TA_GrantAccess_Summry").text().split(';');
            var Comments = $("#TA_Comments_Summry").text();

            ProjectInfo.push(DoorsPaths);
            ProjectInfo.push(TrackNReleasePrjName);
            ProjectInfo.push(ProjectProxies);
            ProjectInfo.push(Comments);

            $.ajax({
                type: "POST",
                url: baseUrl() + "/ProjectConfiguration/ConfigureProject",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({ "PrjConfigObj": ProjectInfo }),
                dataType: "json",
                success: function (JsonData) {
                    alert("success");
                },
                error: function (retVal) {
                    alert("fail");
                }
            });
        }

我的控制器中未检索到上面的代码数据。它对所有字段都是null。

如何在对象内推送对象? 如何将列表值添加到对象?

1 个答案:

答案 0 :(得分:0)

我需要将数据分配给嵌套对象如下:

                var ProjectInfo = {};
                var MCRIDInfo = {};
                MCRIDInfo["MCRID"] = $("#LBL_McrID_Summry").text();
                MCRIDInfo["ProjectName"] = $("#LBL_ProjName_Summry").text();
                MCRIDInfo["ProgramID"] = $("#LBL_PID_Summry").text();
                MCRIDInfo["Division"] = $("#LBL_Division_Summry").text();
                MCRIDInfo["ProjectManager"] = $("#LBL_PJM_Summry").text();
                MCRIDInfo["Generation"] = $("#LBL_Gen_Summry").text();               

                var DoorsPaths = $("#TA_DorsPath_Summry").text().split(';');
                var TrackNReleasePrjName = $("#TA_TRPrjName_Summry").text().split(';');
                var ProjectProxies = $("#TA_GrantAccess_Summry").text().split(';');
                var Comments = $("#TA_Comments_Summry").text();

                ProjectInfo["MCRIDInfo"] = MCRIDInfo;
                ProjectInfo["DoorsPaths"] = DoorsPaths;
                ProjectInfo["TrackNReleasePrjName"] = TrackNReleasePrjName;
                ProjectInfo["ProjectProxies"] = ProjectProxies;
                ProjectInfo["Comments"] = Comments;

注意:对象的名称参数应与Modal类属性名称相同。