我需要从数据库构建一个动态表单。我有以下实体来动态定义表单字段:
public class FormField {
public int ID { get; set; }
public string Name { get; set; }
public string Type { get; set; } // Possible values are: 'Radio','Combo','Text'. A dropdown will be created for a Combo type of element, a radio set for Radio type of element and a text input for Text type of element.
public string Options { get; set; } // Only relevant in case of Radio/Combo type
public string Default { get; set; } // Default value in case of Type 'Text' and selected value in case of Type 'Radio/Combo'
public string Blankout { get; set; }// An expression to define when this field should be hidden
}
/* A sample JSON array (from the DB) to build the form would be:
[
{ Name:"Gender", Type:"radio", Options:["Male","Female","Unknown"], Default:"Male", Blankout:"Never" },
{ Name:"Age", Type:"text", Options:"None", Default:15, Blankout:"Never" },
{ Name:"Neighbourhood", Type:"Combo", Options:["Eastern","Western","Northern","Southern","Central"], Default:"Central", Blankout:"if (Age < 40 or Voted='Obama')" },
{ Name:"Voted", Type:"Combo", Options:["Obama","Romney","Harry Potter"], Default:"Harry Potter", Blankout:"if ((Gender='Female' and Age < 15) or Neighbourhood='Eastern'" }
]
*/
我可以从 DB 中的“FormField”记录构建动态表单,但问题是我需要跟踪任何表单字段的值的变化,当值发生变化时,我需要将所有表单数据发送到服务器(异步)以评估服务器上的“Blankout”公式。如果我在没有 KnockoutJS 的情况下执行此更改跟踪,它不会响应并且变得非常复杂。我已经阅读了几个 KnockoutJS 教程,但无法弄清楚如何针对这个特定问题组织我的 ViewModel。
任何帮助,将不胜感激。
更新 1
我尝试使用以下代码将此表单数据发布到控制器:
$.ajax({
type: "POST",
url: "/MyController/GetBlankoutElements",
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(ko.toJSON(self)),
success: function(result) {
alert(result);
//self.HiddenElements(result.split(','));
}
});
在我的控制器中,我尝试了以下代码:
[HttpPost]
public ActionResult GetBlankoutElements(List<MyFieldViewModel> Fields)
{
return Json(Fields); // List, Fields is null here
}
她是 MyFieldViewModel 类的样子:
public class MyFieldViewModel
{
public string Title { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string Default { get; set; }
public string[] Options { get; set; }
}
我已经尝试过通过 JSON 将对象数组发布到 ASP.Net MVC3 中描述的技巧
以下是我执行alert(ko.toJSON(self))时打印出来的 Json 数据
{"Fields":
[{"Title":"CCType","Name":"CCType","Type":"Radio","Default":"Enterprise","Options":["Enterprise","Express","CVP","PCCE"]},{"Title":"Industry","Name":"Industry","Type":"Combo","Default":"Banks","Options":["Banks","ServiceProvider","Outsourcer","Airlines","Utilities","Government","Retail"]},{"Title":"Customer Lab","Name":"CustomerLab","Type":"Combo","Default":"0","Options":["0","1"]},{"Title":"No of Agents","Name":"Agents","Type":"Text","Default":"if(c.CCType==\"CVP\") then 10 else 25","Options":[]},{"Title":"ExpLicType","Name":"ExpLicType","Type":"Radio","Default":"if(c.CCType==\"Express\") then \"Enhanced\" else \"None\"","Options":["None","Premium","Standard","Enhanced"]},{"Title":"Multimedia","Name":"Multimedia","Type":"Combo","Default":"WIM","Options":["None","EIM","WIM","EIM&WIM","BSMediaRouting","MCAL"]}],
"HiddenElements":[]
}
我需要的只是用户选择的字段名称和它的值,即使我将这个 json 数据映射到我的MyFieldViewModel类,我仍然感到困惑,我仍然如何获得选择的 VALUES ?
更新 2 (JSON 数据映射工作)
当我改变
data: JSON.stringify(ko.toJSON(self))
带数据:ko.toJSON(self)
映射在我的控制器上完美运行,您可以在以下屏幕截图中看到:
现在,问题仍然存在,发布表单的全部目的是用用户在表单上的输入更新服务器,即针对每个表单字段元素的值。 如何发布表单字段的当前选定/键入值? 例如,在上面的屏幕截图中,我可以看到默认值,但看不到当前选择的值。