我正在传递视图模型数据以从 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
}
你能告诉我我在哪里做错了吗。提前致谢。