将数据从js传递到控制器问题

时间:2012-12-18 06:48:21

标签: asp.net-mvc-3 knockout.js

我正在传递视图模型数据,以便从js保存到控制器,如

 $(function () {
    $.ajax({
        url: "/api/BranchSurvey/GetBranchLogicConditions?qId=" + questionId + "&isAdd=false",
        success: function (branchLogicdata) {
            BrSViewModel = ko.mapping.fromJS(branchLogicdata);

            $.ajax({
                url: "/api/BranchSurvey/BranchingLogicQuestion?questionId=" + questionId,
                success: function (branchLogicQuestion) {
                    BrSViewModel.Question = branchLogicQuestion;

                    ko.applyBindings(BrSViewModel, document.getElementById('mainDiv'));
               }
            });

        }
    });

    $("#btnSaveLogic").click(function () {
            debugger;
            $.ajax({
                url: "/api/BranchSurvey/SaveBranchLogicConditions",
                type: 'post',
                data: ko.toJSON(BrSViewModel),
                success: function (data) {
                    branchingSurveyPopupBody.empty();
                    branchingSurveyPopup.dialog('destroy');
                }
            });
            return false;
        });
});

在控制器中

public class BranchSurveyController : ApiController
    {
 [HttpPost]
        public void SaveBranchLogicConditions(BranchLogic objBranchSurvey)
        {

            string str = "<condition>";
            string qstText = "";
            int count = 0;
            foreach (BranchCondiotionXml oxml in objBranchSurvey.ListBranchXML)
            {
               ..... (something)
            }
      }
 } 

当我在浏览器中调试脚本时,我在viewmodel BrSViewModel中获取值。但当它进入控制器时,所有数据都变空了。

我的实体是

public class BranchLogic
    {
        #region private members

        List<BranchCondiotionXml> lstBranchXML = new List<BranchCondiotionXml>();
        Question _question = new Question();
        string targetQuestionCondition = string.Empty;
        string thenTargetQuestion = string.Empty;
        string sucessQuestion = string.Empty;
        string thenTargetUrl = string.Empty;
        string elseTargetQuestionCondition = string.Empty;
        string elseTargetQuestion = string.Empty;
        string elseSuccessQuestion = string.Empty;
        string elseTargetUrl = string.Empty; 

        #endregion

        #region public members
        public List<BranchCondiotionXml> ListBranchXML
        {
            get { return lstBranchXML; }
            set { lstBranchXML = value; }
        }
        public Question Question
        {
            get { return _question; }
            set { _question = value; }
        }
        public string TargetQuestionCondition
        {
            get { return targetQuestionCondition; }
            set { targetQuestionCondition = value; }
        }

        public string ThenTargetQuestion
        {
            get { return thenTargetQuestion; }
            set { thenTargetQuestion = value; }
        }

        public string SucessQuestion
        {
            get { return sucessQuestion; }
            set { sucessQuestion = value; }
        }

        public string ThenTargetUrl
        {
            get { return thenTargetUrl; }
            set { thenTargetUrl = value; }
        }

        public string ElseTargetQuestionCondition
        {
            get { return elseTargetQuestionCondition; }
            set { elseTargetQuestionCondition = value; }
        }

        public string ElseTargetQuestion
        {
            get { return elseTargetQuestion; }
            set { elseTargetQuestion = value; }
        }

        public string ElseSuccessQuestion
        {
            get { return elseSuccessQuestion; }
            set { elseSuccessQuestion = value; }
        }

        public string ElseTargetUrl
        {
            get { return elseTargetUrl; }
            set { elseTargetUrl = value; }
        }
        #endregion

    }

请你告诉我我在哪里做错了。提前谢谢。

0 个答案:

没有答案