MVC4 js里给对象赋值

如果需要用js给control传一个对象,那么对象的属性在c#的model必须加public,不然在js赋值时赋不了的,但是不报错,等你调试到control时,这些属性无聊如何都是null,这样会很郁闷的。

controller方法:

 [HttpPost]
        public JsonResult SaveSelectedAcPoint(AcupuncturePointCriteriaInfo oJsonInfo)
        {
            bool isSeccess = true;// this._iConsultation.SaveMyCustomizeCompound(oJsonInfo);
            // bool message =  isSeccess;//== true ? "添加成功" : "添加失败";
            return new JsonResult
            {
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                Data = new
                {
                    isSeccess
                }
            };
        }

 model:

 public class AcupuncturePointCriteriaEntity {
        //public int ClientRegisterID { get; set; }
        //public int CaseHistoryID { get; set; }
        public int AcupuncturePointID { get; set; }
        public int Supplement { get; set; }
        public string AdditionalTreatment { get; set; }
        public int AcupReinReducing { get; set; }
        public int NeedleTime { get; set; }
    }
   
    public class AcupuncturePointCriteriaInfo : CommonInfo
    {
        public int ClientRegisterID { get; set; }
        public int CaseHistoryID { get; set; }
        List<AcupuncturePointCriteriaEntity> AcupuncturePointList { get; set; }

    }

  List<AcupuncturePointCriteriaEntity> AcupuncturePointList { get; set; }没有加public

所以在js赋值:

function GetAcupuncturePointList() {
    var caseHistoryID = window.opener.document.getElementById("hidCaseHistoryID").value;
    if (caseHistoryID == null || caseHistoryID == "") {
        caseHistoryID = 0;
    }
  
    var clientRegisterId = window.opener.document.getElementById("hidRegisterId").value;
    var needleTime = $("#txtNeedleTime").val();//留针时间
    var acupuncturePointList = [];
    $("#tbSelAcPoint tr:gt(1)").each(function () {
        acupuncturePointList.push({
            //"ClientRegisterID": clientRegisterId,
            //"CaseHistoryID": caseHistoryID,
            "AcupuncturePointID": $(this).find("td:eq(0)").attr("id"),
            "Supplement": $(this).find("td:eq(1) input[name='rdoAcPoint']:checked").val(),
            "AdditionalTreatment": $(this).find("td:eq(2) input[type='checkbox']:checked").map(function () {
                return $(this).val();
            }).get().join(","),
            "AcupReinReducing": $(this).find("td:eq(3) input[name='rdoAcupReinReducing']:checked").val(),
            "NeedleTime": needleTime
        });
    });
    AcupuncturePointCriteriaInfo.CaseHistoryID = caseHistoryID;
    AcupuncturePointCriteriaInfo.ClientRegisterID = clientRegisterId;
    AcupuncturePointCriteriaInfo.AcupuncturePointList = acupuncturePointList;
    return AcupuncturePointCriteriaInfo;
}

 AcupuncturePointCriteriaInfo.AcupuncturePointList = acupuncturePointList;

永远赋值不了,调试到controller后,发现对象的其他属性都有值,但是这个集合永远为null,困扰了我两个夜晚才找到这个原因,后来加了 public终于不是null了,我想很多童鞋都不会注意这个问题

原文地址:https://www.cnblogs.com/yangchuncool/p/3690330.html